public class ParagraphCollection extends NodeCollection
Paragraph nodes.
To learn more, visit the Working with Paragraphs documentation article.
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());
| Modifier and Type | Method and Description |
|---|---|
Node |
get(int index)
Retrieves a
Paragraph at the given index. |
Node[] |
toArray()
Copies all paragraphs from the collection to a new array of paragraphs.
|
add, clear, contains, getContainer, getCount, getCurrentNode, getNextMatchingNode, indexOf, insert, iterator, remove, removeAtpublic Node get(int index)
Paragraph at the given index.
Remarks:
The index is zero-based.
Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.
If index is greater than or equal to the number of items in the list, this returns a null reference.
If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.
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());
get in class NodeCollectionindex - An index into the collection.Paragraph value.public Node[] toArray()
Examples:
Shows how to create an array from a NodeCollection.
Document doc = new Document(getMyDir() + "Paragraphs.docx");
Paragraph[] paras = doc.getFirstSection().getBody().getParagraphs().toArray();
Assert.assertEquals(22, paras.length);
Shows how to use "hot remove" to remove a node during enumeration.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("The first paragraph");
builder.writeln("The second paragraph");
builder.writeln("The third paragraph");
builder.writeln("The fourth paragraph");
// Remove a node from the collection in the middle of an enumeration.
for (Paragraph para : doc.getFirstSection().getBody().getParagraphs().toArray())
if (para.getRange().getText().contains("third"))
para.remove();
Assert.assertFalse(doc.getText().contains("The third paragraph"));
toArray in class NodeCollection