LayoutEnumerator
Inheritance: java.lang.Object
public class LayoutEnumerator
Enumerates page layout entities of a document. You can use this class to walk over the page layout model. Available properties are type, geometry, text and page index where entity is rendered, as well as overall structure and relationships. Use combination of LayoutCollector.getEntity(com.aspose.words.Node) and getCurrent() / setCurrent(java.lang.Object) move to the entity which corresponds to a document node.
To learn more, visit the Converting to Fixed-page Format documentation article.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Constructors
Constructor | Description |
---|---|
LayoutEnumerator(Document document) | Initializes new instance of this class. |
Methods
Method | Description |
---|---|
get(String key) | Gets a named property of the entity. |
getCurrent() | Gets current position in the page layout model. |
getDocument() | Gets document this instance enumerates. |
getKind() | Gets the kind of the current entity. |
getPageIndex() | Gets the 1-based index of a page which contains the current entity. |
getRectangle() | Returns the bounding rectangle of the current entity relative to the page top left corner (in points). |
getText() | Gets text of the current span entity. |
getType() | Gets the type of the current entity. |
moveFirstChild() | Moves to the first child entity. |
moveLastChild() | Moves to the last child entity. |
moveNext() | Moves to the next sibling entity in visual order. |
moveNextLogical() | Moves to the next sibling entity in a logical order. |
moveParent() | Moves to the parent entity. |
moveParent(int types) | |
movePrevious() | Moves to the previous sibling entity. |
movePreviousLogical() | Moves to the previous sibling entity in a logical order. |
reset() | Moves the enumerator to the first page of the document. |
setCurrent(Object value) | Sets current position in the page layout model. |
LayoutEnumerator(Document document)
public LayoutEnumerator(Document document)
Initializes new instance of this class.
Remarks:
If page layout model of the document hasn’t been built the enumerator calls Document.updatePageLayout() to build it.
Whenever document is updated and new page layout model is created, a new enumerator must be used to access it.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Parameters:
Parameter | Type | Description |
---|---|---|
document | Document | A document whose page layout model to enumerate. |
get(String key)
public Object get(String key)
Gets a named property of the entity.
Remarks:
This is currently used to get font properties of spans. See Font class for possible properties names. Not all properties are supported.
Parameters:
Parameter | Type | Description |
---|---|---|
key | java.lang.String | A name of the property (case-sensitive). |
Returns: java.lang.Object - Null if property is not available, otherwise value of the property.
getCurrent()
public Object getCurrent()
Gets current position in the page layout model. This property returns an opaque object which corresponds to the current layout entity.
Examples:
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) 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());
Returns: java.lang.Object - Current position in the page layout model.
getDocument()
public Document getDocument()
Gets document this instance enumerates.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: Document - Document this instance enumerates.
getKind()
public String getKind()
Gets the kind of the current entity. This can be an empty string but never null .
Remarks:
This is a more specific type of the current entity, e.g. bookmark span has LayoutEntityType.SPAN type and may have either a BOOKMARKSTART or BOOKMARKEND kind.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: java.lang.String - The kind of the current entity.
getPageIndex()
public int getPageIndex()
Gets the 1-based index of a page which contains the current entity.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: int - The 1-based index of a page which contains the current entity.
getRectangle()
public Rectangle2D.Float getRectangle()
Returns the bounding rectangle of the current entity relative to the page top left corner (in points).
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: java.awt.geom.Rectangle2D.Float - The bounding rectangle of the current entity relative to the page top left corner (in points).
getText()
public String getText()
Gets text of the current span entity. Throws for other entity types.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: java.lang.String - Text of the current span entity.
getType()
public int getType()
Gets the type of the current entity.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: int - The type of the current entity. The returned value is a bitwise combination of LayoutEntityType constants.
moveFirstChild()
public boolean moveFirstChild()
Moves to the first child entity.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: boolean
moveLastChild()
public boolean moveLastChild()
Moves to the last child entity.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: boolean
moveNext()
public boolean moveNext()
Moves to the next sibling entity in visual order. When iterating lines of a paragraph broken across pages this method will not move to the next page but rather move to the next entity on the same page.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: boolean
moveNextLogical()
public boolean moveNextLogical()
Moves to the next sibling entity in a logical order. When iterating lines of a paragraph broken across pages this method will move to the next line even if it resides on another page.
Remarks:
Note that all LayoutEntityType.SPAN entities are linked together thus if getCurrent() / setCurrent(java.lang.Object) entity is span repeated calling of this method will iterates complete story of the document.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: boolean
moveParent()
public boolean moveParent()
Moves to the parent entity.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: boolean
moveParent(int types)
public boolean moveParent(int types)
Parameters:
Parameter | Type | Description |
---|---|---|
types | int |
Returns: boolean
movePrevious()
public boolean movePrevious()
Moves to the previous sibling entity.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: boolean
movePreviousLogical()
public boolean movePreviousLogical()
Moves to the previous sibling entity in a logical order. When iterating lines of a paragraph broken across pages this method will move to the previous line even if it resides on another page.
Remarks:
Note that all LayoutEntityType.SPAN entities are linked together thus if getCurrent() / setCurrent(java.lang.Object) entity is span repeated calling of this method will iterates complete story of the document.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
Returns: boolean
reset()
public void reset()
Moves the enumerator to the first page of the document.
Examples:
Shows ways of traversing a document’s layout entities.
public void layoutEnumerator() throws Exception {
// Open a document that contains a variety of layout entities.
// Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
// Each layout entity has a rectangular space that it occupies in the document body.
Document doc = new Document(getMyDir() + "Layout entities.docx");
// Create an enumerator that can traverse these entities like a tree.
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.assertEquals(doc, layoutEnumerator.getDocument());
layoutEnumerator.moveParent(LayoutEntityType.PAGE);
Assert.assertEquals(LayoutEntityType.PAGE, layoutEnumerator.getType());
Assert.assertThrows(IllegalStateException.class, () -> System.out.println(layoutEnumerator.getText()));
// We can call this method to make sure that the enumerator will be at the first layout entity.
layoutEnumerator.reset();
// There are two orders that determine how the layout enumerator continues traversing layout entities
// when it encounters entities that span across multiple pages.
// 1 - In visual order:
// When moving through an entity's children that span multiple pages,
// page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
System.out.println("Traversing from first to last, elements between pages separated:");
traverseLayoutForward(layoutEnumerator, 1);
// Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
System.out.println("Traversing from last to first, elements between pages separated:");
traverseLayoutBackward(layoutEnumerator, 1);
// 2 - In logical order:
// When moving through an entity's children that span multiple pages,
// the enumerator will move between pages to traverse all the child entities.
System.out.println("Traversing from first to last, elements between pages mixed:");
traverseLayoutForwardLogical(layoutEnumerator, 1);
System.out.println("Traversing from last to first, elements between pages mixed:");
traverseLayoutBackwardLogical(layoutEnumerator, 1);
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNext());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Visual" order.
///
private static void traverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackward(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePrevious());
}
///
/// Enumerate through layoutEnumerator's layout entity collection front-to-back,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveFirstChild()) {
traverseLayoutForwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.moveNextLogical());
}
///
/// Enumerate through layoutEnumerator's layout entity collection back-to-front,
/// in a depth-first manner, and in the "Logical" order.
///
private static void traverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth) throws Exception {
do {
printCurrentEntity(layoutEnumerator, depth);
if (layoutEnumerator.moveLastChild()) {
traverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
layoutEnumerator.moveParent();
}
} while (layoutEnumerator.movePreviousLogical());
}
///
/// Print information about layoutEnumerator's current entity to the console, while indenting the text with tab characters
/// based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
/// The rectangle that we process at the end represents the area and location that the entity takes up in the document.
///
private static void printCurrentEntity(LayoutEnumerator layoutEnumerator, int indent) throws Exception {
String tabs = StringUtils.repeat("\t", indent);
System.out.println(layoutEnumerator.getKind().equals("")
? MessageFormat.format("{0}-> Entity type: {1}", tabs, layoutEnumerator.getType())
: MessageFormat.format("{0}-> Entity type & kind: {1}, {2}", tabs, layoutEnumerator.getType(), layoutEnumerator.getKind()));
// Only spans can contain text.
if (layoutEnumerator.getType() == LayoutEntityType.SPAN)
System.out.println("{tabs} Span contents: \"{layoutEnumerator.Text}\"");
Rectangle2D.Float leRect = layoutEnumerator.getRectangle();
System.out.println(MessageFormat.format("{0} Rectangle dimensions {1}x{2}, X={3} Y={4}", tabs, leRect.getWidth(), leRect.getHeight(), leRect.getX(), leRect.getY()));
System.out.println(MessageFormat.format("{0} Page {1}", tabs, layoutEnumerator.getPageIndex()));
}
setCurrent(Object value)
public void setCurrent(Object value)
Sets current position in the page layout model. This property returns an opaque object which corresponds to the current layout entity.
Examples:
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) 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());
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.Object | Current position in the page layout model. |