java.lang.Object
com.aspose.words.LayoutCollector
public class LayoutCollector
This class allows to compute page numbers of document nodes. To learn more, visit the Converting to Fixed-page Format documentation article. When you create a You will be able to find out on which page a particular document node (e.g. run, paragraph or table cell) is located
by using the When you no longer need to collect layout information, it is best to set the Example:null
to avoid unnecessary collection of more layout mappings.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());
| Constructor Summary |
|---|
LayoutCollector(Document doc)
Initializes an instance of this class. |
| Property Getters/Setters Summary | ||
|---|---|---|
Document | getDocument() | |
void | setDocument(Document value) | |
| Gets or sets the document this collector instance is attached to. | ||
| Method Summary | ||
|---|---|---|
void | clear() | |
| Clears all collected layout data. Call this method after document was manually updated, or layout was rebuilt. | ||
int | getEndPageIndex(Node node) | |
| Gets 1-based index of the page where node ends. Returns 0 if node cannot be mapped to a page. | ||
java.lang.Object | getEntity(Node node) | |
|
Returns an opaque position of the |
||
int | getNumPagesSpanned(Node node) | |
|
Gets number of pages the specified node spans. 0 if node is within a single page.
This is the same as |
||
int | getStartPageIndex(Node node) | |
| Gets 1-based index of the page where node begins. Returns 0 if node cannot be mapped to a page. | ||
| Constructor Detail |
|---|
public LayoutCollector(Document doc)
doc - The document to which this collector instance will be attached to.Example:
Shows how to see the the ranges of pages that a node spans.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());| Property Getters/Setters Detail |
|---|
getDocument/setDocument | |
public Document getDocument() / public void setDocument(Document value) | |
null afterwards,
otherwise the collector continues to accumulate information from subsequent rebuilds of the document's page layout.
Example:
Shows how to see the the ranges of pages that a node spans.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());| Method Detail |
|---|
clear | |
public void clear() | |
Example:
Shows how to see the the ranges of pages that a node spans.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());getEndPageIndex | |
public int getEndPageIndex(Node node) throws java.lang.Exception | |
Example:
Shows how to see the the ranges of pages that a node spans.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());getEntity | |
public java.lang.Object getEntity(Node node) throws java.lang.Exception | |
This method works for only
Note that the entity returned for a
If you need to navigate to a
If you need to navigate to a
Example:
Shows how to see the the ranges of pages that a node spans.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());getNumPagesSpanned | |
public int getNumPagesSpanned(Node node) throws java.lang.Exception | |
Example:
Shows how to see the the ranges of pages that a node spans.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());getStartPageIndex | |
public int getStartPageIndex(Node node) throws java.lang.Exception | |
Example:
Shows how to see the the ranges of pages that a node spans.
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.assertEquals(doc, layoutCollector.getDocument());
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.assertEquals(0, layoutCollector.getNumPagesSpanned(doc));
layoutCollector.clear();
doc.updatePageLayout();
Assert.assertEquals(5, layoutCollector.getNumPagesSpanned(doc));
// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.getChildNodes(NodeType.ANY, true);
for (Node node : (Iterable<Node>) nodes) {
System.out.println(MessageFormat.format("-> NodeType.{0}: ", node.getNodeType()));
System.out.println(MessageFormat.format("\tStarts on page {0}, ends on page {1},", layoutCollector.getStartPageIndex(node), layoutCollector.getEndPageIndex(node)) +
MessageFormat.format(" spanning {0} pages.", layoutCollector.getNumPagesSpanned(node)));
}
// We can iterate over the layout entities using a LayoutEnumerator.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
// The LayoutEnumerator can traverse the collection of layout entities like a tree.
// We can also apply it to any node's corresponding layout entity.
layoutEnumerator.setCurrent(layoutCollector.getEntity(doc.getChild(NodeType.PARAGRAPH, 1, true)));
Assert.assertEquals(LayoutEntityType.SPAN, layoutEnumerator.getType());
Assert.assertEquals("¶", layoutEnumerator.getText());