public class Run extends Inline
To learn more, visit the Programming with Documents documentation article.
Remarks:
All text of the document is stored in runs of text.
Run can only be a child of Paragraph or inline StructuredDocumentTag.
Examples:
Shows how to format a run of text using its font property.
Document doc = new Document();
Run run = new Run(doc, "Hello world!");
Font font = run.getFont();
font.setName("Courier New");
font.setSize(36.0);
font.setHighlightColor(Color.YELLOW);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(run);
doc.save(getArtifactsDir() + "Font.CreateFormattedRun.docx");
Shows how to construct an Aspose.Words document by hand.
Document doc = new Document();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc.removeAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
Section section = new Section(doc);
doc.appendChild(section);
// Set some page setup properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
Body body = new Body(doc);
section.appendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
Paragraph para = new Paragraph(doc);
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
body.appendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!", doc.getText().trim());
doc.save(getArtifactsDir() + "Section.CreateManually.docx");
Shows how to add, update and delete child nodes in a CompositeNode's collection of children.
Document doc = new Document();
// An empty document, by default, has one paragraph.
Assert.assertEquals(1, doc.getFirstSection().getBody().getParagraphs().getCount());
// Composite nodes such as our paragraph can contain other composite and inline nodes as children.
Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph();
Run paragraphText = new Run(doc, "Initial text. ");
paragraph.appendChild(paragraphText);
// Create three more run nodes.
Run run1 = new Run(doc, "Run 1. ");
Run run2 = new Run(doc, "Run 2. ");
Run run3 = new Run(doc, "Run 3. ");
// The document body will not display these runs until we insert them into a composite node
// that itself is a part of the document's node tree, as we did with the first run.
// We can determine where the text contents of nodes that we insert
// appears in the document by specifying an insertion location relative to another node in the paragraph.
Assert.assertEquals("Initial text.", paragraph.getText().trim());
// Insert the second run into the paragraph in front of the initial run.
paragraph.insertBefore(run2, paragraphText);
Assert.assertEquals("Run 2. Initial text.", paragraph.getText().trim());
// Insert the third run after the initial run.
paragraph.insertAfter(run3, paragraphText);
Assert.assertEquals("Run 2. Initial text. Run 3.", paragraph.getText().trim());
// Insert the first run to the start of the paragraph's child nodes collection.
paragraph.prependChild(run1);
Assert.assertEquals("Run 1. Run 2. Initial text. Run 3.", paragraph.getText().trim());
Assert.assertEquals(4, paragraph.getChildNodes(NodeType.ANY, true).getCount());
// We can modify the contents of the run by editing and deleting existing child nodes.
((Run) paragraph.getChildNodes(NodeType.RUN, true).get(1)).setText("Updated run 2. ");
paragraph.getChildNodes(NodeType.RUN, true).remove(paragraphText);
Assert.assertEquals("Run 1. Updated run 2. Run 3.", paragraph.getText().trim());
Assert.assertEquals(3, paragraph.getChildNodes(NodeType.ANY, true).getCount());
| Constructor and Description |
|---|
Run(DocumentBase doc)
Initializes a new instance of the
Run class. |
Run(DocumentBase doc,
java.lang.String text)
Initializes a new instance of the Run class.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
accept(DocumentVisitor visitor)
Accepts a visitor.
|
int |
getNodeType()
Returns
NodeType.RUN. |
PhoneticGuide |
getPhoneticGuide()
Gets a
getPhoneticGuide() object. |
java.lang.String |
getText()
Gets the text of the run.
|
boolean |
isPhoneticGuide()
Gets a boolean value indicating either the run is a phonetic guide.
|
void |
setText(java.lang.String value)
Sets the text of the run.
|
clearRunAttrs, fetchInheritedRunAttr, getDirectRunAttr, getDirectRunAttr, getDocument_IInline, getFont, getParentParagraph_IInline, getParentParagraph, isDeleteRevision, isFormatRevision, isInsertRevision, isMoveFromRevision, isMoveToRevision, removeMoveRevisions, removeRunAttr, setRunAttrdeepClone, getAncestor, getAncestor, getCustomNodeId, getDocument, getNextSibling, getParentNode, getPreviousSibling, getRange, isComposite, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolpublic Run(DocumentBase doc)
Run class.
Remarks:
When Run is created, it belongs to the specified document, but is not yet part of the document and Node.getParentNode() is null.
To append Run to the document use CompositeNode.insertAfter(com.aspose.words.Node, com.aspose.words.Node) or CompositeNode.insertBefore(com.aspose.words.Node, com.aspose.words.Node) on the paragraph where you want the run inserted.
Examples:
Shows how to construct an Aspose.Words document by hand.
Document doc = new Document();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc.removeAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
Section section = new Section(doc);
doc.appendChild(section);
// Set some page setup properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
Body body = new Body(doc);
section.appendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
Paragraph para = new Paragraph(doc);
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
body.appendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!", doc.getText().trim());
doc.save(getArtifactsDir() + "Section.CreateManually.docx");
doc - The owner document.public Run(DocumentBase doc, java.lang.String text)
Remarks:
When Run is created, it belongs to the specified document, but is not yet part of the document and Node.getParentNode() is null.
To append Run to the document use CompositeNode.insertAfter(com.aspose.words.Node, com.aspose.words.Node) or CompositeNode.insertBefore(com.aspose.words.Node, com.aspose.words.Node) on the paragraph where you want the run inserted.
Examples:
Shows how to format a run of text using its font property.
Document doc = new Document();
Run run = new Run(doc, "Hello world!");
Font font = run.getFont();
font.setName("Courier New");
font.setSize(36.0);
font.setHighlightColor(Color.YELLOW);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(run);
doc.save(getArtifactsDir() + "Font.CreateFormattedRun.docx");
doc - The owner document.text - The text of the run.public int getNodeType()
NodeType.RUN.
Examples:
Shows how to traverse a composite node's tree of child nodes.
public void recurseChildren() throws Exception {
Document doc = new Document(getMyDir() + "Paragraphs.docx");
// Any node that can contain child nodes, such as the document itself, is composite.
Assert.assertTrue(doc.isComposite());
// Invoke the recursive function that will go through and print all the child nodes of a composite node.
traverseAllNodes(doc, 0);
}
/// <summary>
/// Recursively traverses a node tree while printing the type of each node
/// with an indent depending on depth as well as the contents of all inline nodes.
/// </summary>
public void traverseAllNodes(CompositeNode parentNode, int depth) {
for (Node childNode = parentNode.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
System.out.println(MessageFormat.format("{0}{1}", String.format(" ", depth), Node.nodeTypeToString(childNode.getNodeType())));
// Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node.
if (childNode.isComposite()) {
System.out.println();
traverseAllNodes((CompositeNode) childNode, depth + 1);
} else if (childNode instanceof Inline) {
System.out.println(MessageFormat.format(" - \"{0}\"", childNode.getText().trim()));
} else {
System.out.println();
}
}
}
getNodeType in class NodeNodeType.RUN. The returned value is one of NodeType constants.public java.lang.String getText()
Examples:
Shows how to construct an Aspose.Words document by hand.
Document doc = new Document();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc.removeAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
Section section = new Section(doc);
doc.appendChild(section);
// Set some page setup properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
Body body = new Body(doc);
section.appendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
Paragraph para = new Paragraph(doc);
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
body.appendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!", doc.getText().trim());
doc.save(getArtifactsDir() + "Section.CreateManually.docx");
public void setText(java.lang.String value)
Examples:
Shows how to construct an Aspose.Words document by hand.
Document doc = new Document();
// A blank document contains one section, one body and one paragraph.
// Call the "RemoveAllChildren" method to remove all those nodes,
// and end up with a document node with no children.
doc.removeAllChildren();
// This document now has no composite child nodes that we can add content to.
// If we wish to edit it, we will need to repopulate its node collection.
// First, create a new section, and then append it as a child to the root document node.
Section section = new Section(doc);
doc.appendChild(section);
// Set some page setup properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// A section needs a body, which will contain and display all its contents
// on the page between the section's header and footer.
Body body = new Body(doc);
section.appendChild(body);
// Create a paragraph, set some formatting properties, and then append it as a child to the body.
Paragraph para = new Paragraph(doc);
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
body.appendChild(para);
// Finally, add some content to do the document. Create a run,
// set its appearance and contents, and then append it as a child to the paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
Assert.assertEquals("Hello World!", doc.getText().trim());
doc.save(getArtifactsDir() + "Section.CreateManually.docx");
value - The text of the run.public boolean isPhoneticGuide()
Examples:
Shows how to get properties of the phonetic guide.
Document doc = new Document(getMyDir() + "Phonetic guide.docx");
RunCollection runs = doc.getFirstSection().getBody().getFirstParagraph().getRuns();
// Use phonetic guide in the Asian text.
Assert.assertEquals(true, runs.get(0).isPhoneticGuide());
PhoneticGuide phoneticGuide = runs.get(0).getPhoneticGuide();
Assert.assertEquals("base", phoneticGuide.getBaseText());
Assert.assertEquals("ruby", phoneticGuide.getRubyText());
public PhoneticGuide getPhoneticGuide()
getPhoneticGuide() object.
Examples:
Shows how to get properties of the phonetic guide.
Document doc = new Document(getMyDir() + "Phonetic guide.docx");
RunCollection runs = doc.getFirstSection().getBody().getFirstParagraph().getRuns();
// Use phonetic guide in the Asian text.
Assert.assertEquals(true, runs.get(0).isPhoneticGuide());
PhoneticGuide phoneticGuide = runs.get(0).getPhoneticGuide();
Assert.assertEquals("base", phoneticGuide.getBaseText());
Assert.assertEquals("ruby", phoneticGuide.getRubyText());
getPhoneticGuide() object.public boolean accept(DocumentVisitor visitor) throws java.lang.Exception
Remarks:
Calls DocumentVisitor.visitRun(com.aspose.words.Run).
For more info see the Visitor design pattern.
Examples:
Shows how to print the node structure of every header and footer in a document.
public void headerFooterToText() throws Exception {
Document doc = new Document(getMyDir() + "DocumentVisitor-compatible features.docx");
HeaderFooterStructurePrinter visitor = new HeaderFooterStructurePrinter();
// When we get a composite node to accept a document visitor, the visitor visits the accepting node,
// and then traverses all the node's children in a depth-first manner.
// The visitor can read and modify each visited node.
doc.accept(visitor);
System.out.println(visitor.getText());
// An alternative way of accessing a document's header/footers section-by-section is by accessing the collection.
HeaderFooter[] headerFooters = doc.getFirstSection().getHeadersFooters().toArray();
Assert.assertEquals(3, headerFooters.length);
}
/// <summary>
/// Traverses a node's non-binary tree of child nodes.
/// Creates a map in the form of a string of all encountered HeaderFooter nodes and their children.
/// </summary>
public static class HeaderFooterStructurePrinter extends DocumentVisitor {
public HeaderFooterStructurePrinter() {
mBuilder = new StringBuilder();
mVisitorIsInsideHeaderFooter = false;
}
public String getText() {
return mBuilder.toString();
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(final Run run) {
if (mVisitorIsInsideHeaderFooter) indentAndAppendLine("[Run] \"" + run.getText() + "\"");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a HeaderFooter node is encountered in the document.
/// </summary>
public int visitHeaderFooterStart(final HeaderFooter headerFooter) {
indentAndAppendLine("[HeaderFooter start] HeaderFooterType: " + headerFooter.getHeaderFooterType());
mDocTraversalDepth++;
mVisitorIsInsideHeaderFooter = true;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called after all the child nodes of a HeaderFooter node have been visited.
/// </summary>
public int visitHeaderFooterEnd(final HeaderFooter headerFooter) {
mDocTraversalDepth--;
indentAndAppendLine("[HeaderFooter end]");
mVisitorIsInsideHeaderFooter = false;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Append a line to the StringBuilder, and indent it depending on how deep the visitor is into the document tree.
/// </summary>
/// <param name="text"></param>
private void indentAndAppendLine(final String text) {
for (int i = 0; i < mDocTraversalDepth; i++) {
mBuilder.append("| ");
}
mBuilder.append(text + "\r\n");
}
private boolean mVisitorIsInsideHeaderFooter;
private int mDocTraversalDepth;
private final StringBuilder mBuilder;
}