|
java.lang.Object
Node
Inline
com.aspose.words.Run
- All Implemented Interfaces:
- java.lang.Cloneable, java.lang.Iterable
public class Run - extends Inline
Represents a run of characters with the same font formatting.
All text of the document is stored in runs of text. Run can only be a child of Paragraph. Example: Shows how to add a formatted run of text to a document using the object model.
// Create an empty document. It contains one empty paragraph.
Document doc = new Document();
// Create a new run of text.
Run run = new Run(doc, "Hello");
// Specify character formatting for the run of text.
Font f = run.getFont();
f.setName("Courier New");
f.setSize(36);
f.setHighlightColor(Color.YELLOW);
// Append the run of text to the end of the first paragraph
// in the body of the first section of the document.
doc.getFirstSection().getBody().getParagraphs().get(0).appendChild(run);Example: Gets all fonts used in a document.
Document doc = new Document(getMyDir() + "Font.Names.doc");
// Select all runs in the document.
NodeCollection<Run> runs = doc.getChildNodes(NodeType.RUN, true, false);
// Use a hashtable so we will keep only unique font names.
Hashtable fontNames = new Hashtable();
for (Run run : runs)
{
// This adds an entry into the hashtable.
// The key is the font name. The value is null, we don't need the value.
fontNames.put(run.getFont().getName(), 0);
}
// There are two fonts used in this document.
Assert.assertEquals(2, fontNames.size());Example: Creates a simple document from scratch using the Aspose.Words object model.
// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();
// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.removeAllChildren();
// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);
// Append the section to the document.
doc.appendChild(section);
// Lets set some properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// The section that we created is empty, lets populate it. The section needs at least the Body node.
Body body = new Body(doc);
section.appendChild(body);
// The body needs to have at least one paragraph.
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document.
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information.
Paragraph para = new Paragraph(doc);
body.appendChild(para);
// We can set some formatting for the paragraph
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// So far we have one empty pararagraph in the document.
// The document is valid and can be saved, but lets add some text before saving.
// Create a new run of text and add it to our paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
// As a matter of interest, you can retrieve text of the whole document and
// see that \u000c is automatically appended. \u000c is the end of section character.
Assert.assertEquals("Hello World!\u000c", doc.getText());
// Save the document.
doc.save(getMyDir() + "Section.CreateFromScratch Out.doc");
|
Constructor Summary |
Run(Document doc)
Initializes a new instance of the Run class.
|
Run(Document doc, java.lang.String text)
Initializes a new instance of the Run class.
|
|