public abstract class Story extends CompositeNode
Paragraph and Table.
To learn more, visit the Logical Levels of Nodes in a Document documentation article.
Remarks:
Text of a Word document is said to consist of several stories. The main text is stored in the main text story represented by Body, each header and footer is stored in a separate story represented by HeaderFooter.
Examples:
Shows how to remove all shapes from a node.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a DocumentBuilder to insert a shape. This is an inline shape,
// which has a parent Paragraph, which is a child node of the first section's Body.
builder.insertShape(ShapeType.CUBE, 100.0, 100.0);
Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 1);
// We can delete all shapes from the child paragraphs of this Body.
Assert.assertEquals(doc.getFirstSection().getBody().getStoryType(), StoryType.MAIN_TEXT);
doc.getFirstSection().getBody().deleteShapes();
Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0);
| Constructor and Description |
|---|
Story() |
| Modifier and Type | Method and Description |
|---|---|
Paragraph |
appendParagraph(java.lang.String text)
A shortcut method that creates a
Paragraph object with optional text and appends it to the end of this object. |
void |
deleteShapes()
Deletes all shapes from the text of this story.
|
Paragraph |
getFirstParagraph()
Gets the first paragraph in the story.
|
Paragraph |
getLastParagraph()
Gets the last paragraph in the story.
|
ParagraphCollection |
getParagraphs()
Gets a collection of paragraphs that are immediate children of the story.
|
int |
getStoryType()
Gets the type of this story.
|
TableCollection |
getTables()
Gets a collection of tables that are immediate children of the story.
|
acceptChildren, acceptCore, acceptEnd, acceptStart, appendChild, coreRemoveSelfOnly, getChild, getChildNodes, getContainer, getCount, getCurrentNode, getFirstChild, getLastChild, getNextMatchingNode, getText, hasChildNodes, indexOf, insertAfter, insertBefore, isComposite, iterator, prependChild, removeAllChildren, removeChild, removeSmartTags, selectNodes, selectSingleNodeaccept, deepClone, getAncestor, getAncestor, getCustomNodeId, getDocument, getNextSibling, getNodeType, getParentNode, getPreviousSibling, getRange, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolpublic int getStoryType()
Examples:
Shows how to remove all shapes from a node.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a DocumentBuilder to insert a shape. This is an inline shape,
// which has a parent Paragraph, which is a child node of the first section's Body.
builder.insertShape(ShapeType.CUBE, 100.0, 100.0);
Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 1);
// We can delete all shapes from the child paragraphs of this Body.
Assert.assertEquals(doc.getFirstSection().getBody().getStoryType(), StoryType.MAIN_TEXT);
doc.getFirstSection().getBody().deleteShapes();
Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0);
StoryType constants.public Paragraph getFirstParagraph()
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 create and format a text box.
Document doc = new Document();
// Create a floating text box.
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
textBox.setWrapType(WrapType.NONE);
textBox.setHeight(50.0);
textBox.setWidth(200.0);
// Set the horizontal, and vertical alignment of the text inside the shape.
textBox.setHorizontalAlignment(HorizontalAlignment.CENTER);
textBox.setVerticalAlignment(VerticalAlignment.TOP);
// Add a paragraph to the text box and add a run of text that the text box will display.
textBox.appendChild(new Paragraph(doc));
Paragraph para = textBox.getFirstParagraph();
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
Run run = new Run(doc);
run.setText("Hello world!");
para.appendChild(run);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);
doc.save(getArtifactsDir() + "Shape.CreateTextBox.docx");
public Paragraph getLastParagraph()
Examples:
Shows how to move a DocumentBuilder's cursor position to a specified node.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Run 1. ");
// The document builder has a cursor, which acts as the part of the document
// where the builder appends new nodes when we use its document construction methods.
// This cursor functions in the same way as Microsoft Word's blinking cursor,
// and it also always ends up immediately after any node that the builder just inserted.
// To append content to a different part of the document,
// we can move the cursor to a different node with the "MoveTo" method.
builder.moveTo(doc.getFirstSection().getBody().getFirstParagraph().getRuns().get(0));
// The cursor is now in front of the node that we moved it to.
// Adding a second run will insert it in front of the first run.
builder.writeln("Run 2. ");
Assert.assertEquals("Run 2. \rRun 1.", doc.getText().trim());
// Move the cursor to the end of the document to continue appending text to the end as before.
builder.moveTo(doc.getLastSection().getBody().getLastParagraph());
builder.writeln("Run 3. ");
Assert.assertEquals("Run 2. \rRun 1. \rRun 3.", doc.getText().trim());
public ParagraphCollection getParagraphs()
Examples:
Shows how to check whether a paragraph is a move revision.
Document doc = new Document(getMyDir() + "Revisions.docx");
// This document contains "Move" revisions, which appear when we highlight text with the cursor,
// and then drag it to move it to another location
// while tracking revisions in Microsoft Word via "Review" -> "Track changes".
Assert.assertEquals(6, IterableUtils.countMatches(doc.getRevisions(), r -> r.getRevisionType() == RevisionType.MOVING));
ParagraphCollection paragraphs = doc.getFirstSection().getBody().getParagraphs();
// Move revisions consist of pairs of "Move from", and "Move to" revisions.
// These revisions are potential changes to the document that we can either accept or reject.
// Before we accept/reject a move revision, the document
// must keep track of both the departure and arrival destinations of the text.
// The second and the fourth paragraph define one such revision, and thus both have the same contents.
Assert.assertEquals(paragraphs.get(1).getText(), paragraphs.get(3).getText());
// The "Move from" revision is the paragraph where we dragged the text from.
// If we accept the revision, this paragraph will disappear,
// and the other will remain and no longer be a revision.
Assert.assertTrue(paragraphs.get(1).isMoveFromRevision());
// The "Move to" revision is the paragraph where we dragged the text to.
// If we reject the revision, this paragraph instead will disappear, and the other will remain.
Assert.assertTrue(paragraphs.get(3).isMoveToRevision());
public TableCollection getTables()
Examples:
Shows how to remove the first and last rows of all tables in a document.
Document doc = new Document(getMyDir() + "Tables.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
Assert.assertEquals(5, tables.get(0).getRows().getCount());
Assert.assertEquals(4, tables.get(1).getRows().getCount());
for (Table table : tables) {
if (table.getFirstRow() != null) {
table.getFirstRow().remove();
}
if (table.getLastRow() != null) {
table.getLastRow().remove();
}
}
Assert.assertEquals(3, tables.get(0).getRows().getCount());
Assert.assertEquals(2, tables.get(1).getRows().getCount());
public void deleteShapes()
Examples:
Shows how to remove all shapes from a node.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a DocumentBuilder to insert a shape. This is an inline shape,
// which has a parent Paragraph, which is a child node of the first section's Body.
builder.insertShape(ShapeType.CUBE, 100.0, 100.0);
Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 1);
// We can delete all shapes from the child paragraphs of this Body.
Assert.assertEquals(doc.getFirstSection().getBody().getStoryType(), StoryType.MAIN_TEXT);
doc.getFirstSection().getBody().deleteShapes();
Assert.assertEquals(doc.getChildNodes(NodeType.SHAPE, true).getCount(), 0);
public Paragraph appendParagraph(java.lang.String text)
Paragraph object with optional text and appends it to the end of this object.
Examples:
Shows how to create a header and a footer.
Document doc = new Document();
// Create a header and append a paragraph to it. The text in that paragraph
// will appear at the top of every page of this section, above the main body text.
HeaderFooter header = new HeaderFooter(doc, HeaderFooterType.HEADER_PRIMARY);
doc.getFirstSection().getHeadersFooters().add(header);
Paragraph para = header.appendParagraph("My header.");
Assert.assertTrue(header.isHeader());
Assert.assertTrue(para.isEndOfHeaderFooter());
// Create a footer and append a paragraph to it. The text in that paragraph
// will appear at the bottom of every page of this section, below the main body text.
HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
doc.getFirstSection().getHeadersFooters().add(footer);
para = footer.appendParagraph("My footer.");
Assert.assertFalse(footer.isHeader());
Assert.assertTrue(para.isEndOfHeaderFooter());
Assert.assertEquals(para.getParentStory(), footer);
Assert.assertEquals(para.getParentSection(), footer.getParentSection());
Assert.assertEquals(header.getParentSection(), footer.getParentSection());
doc.save(getArtifactsDir() + "HeaderFooter.Create.docx");
text - The text for the paragraph. Can be null or empty string.