public class SubDocument extends Node
To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.
Remarks:
In this version of Aspose.Words, SubDocument nodes do not provide public methods and properties to create or modify a subdocument. In this version you are not able to instantiate SubDocument nodes or modify existing except deleting them.
SubDocument can only be a child of Paragraph.
Examples:
Shows how to access a master document's subdocument.
Document doc = new Document(getMyDir() + "Master document.docx");
NodeCollection subDocuments = doc.getChildNodes(NodeType.SUB_DOCUMENT, true);
// This node serves as a reference to an external document, and its contents cannot be accessed.
SubDocument subDocument = (SubDocument) subDocuments.get(0);
Assert.assertFalse(subDocument.isComposite());
| Modifier and Type | Method and Description |
|---|---|
boolean |
accept(DocumentVisitor visitor)
Accepts a visitor.
|
int |
getNodeType()
Returns
NodeType.SUB_DOCUMENT. |
deepClone, getAncestor, getAncestor, getCustomNodeId, getDocument, getNextSibling, getParentNode, getPreviousSibling, getRange, getText, isComposite, memberwiseClone, nextPreOrder, nodeTypeToString, previousPreOrder, remove, setCustomNodeId, toString, toString, toString, visitorActionToBoolpublic int getNodeType()
NodeType.SUB_DOCUMENT.
Examples:
Shows how to access a master document's subdocument.
Document doc = new Document(getMyDir() + "Master document.docx");
NodeCollection subDocuments = doc.getChildNodes(NodeType.SUB_DOCUMENT, true);
// This node serves as a reference to an external document, and its contents cannot be accessed.
SubDocument subDocument = (SubDocument) subDocuments.get(0);
Assert.assertFalse(subDocument.isComposite());
getNodeType in class NodeNodeType.SUB_DOCUMENT. The returned value is one of NodeType constants.public boolean accept(DocumentVisitor visitor) throws java.lang.Exception
Remarks:
Enumerates over this node and all of its children. Each node calls a corresponding method on DocumentVisitor.
For more info see the Visitor design pattern.
Examples:
Shows how to use a document visitor to print a document's node structure.
public void docStructureToText() throws Exception {
Document doc = new Document(getMyDir() + "DocumentVisitor-compatible features.docx");
DocStructurePrinter visitor = new DocStructurePrinter();
// When we get a composite node to accept a document visitor, the visitor visits the accepting node,
// and then traverses all the node's children in a depth-first manner.
// The visitor can read and modify each visited node.
doc.accept(visitor);
System.out.println(visitor.getText());
}
/// <summary>
/// Traverses a node's tree of child nodes.
/// Creates a map of this tree in the form of a string.
/// </summary>
public static class DocStructurePrinter extends DocumentVisitor {
public DocStructurePrinter() {
mAcceptingNodeChildTree = new StringBuilder();
}
public String getText() {
return mAcceptingNodeChildTree.toString();
}
/// <summary>
/// Called when a Document node is encountered.
/// </summary>
public int visitDocumentStart(Document doc) {
int childNodeCount = doc.getChildNodes(NodeType.ANY, true).getCount();
indentAndAppendLine("[Document start] Child nodes: " + childNodeCount);
mDocTraversalDepth++;
// Allow the visitor to continue visiting other nodes.
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called after all the child nodes of a Document node have been visited.
/// </summary>
public int visitDocumentEnd(Document doc) {
mDocTraversalDepth--;
indentAndAppendLine("[Document end]");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Section node is encountered in the document.
/// </summary>
public int visitSectionStart(final Section section) {
// Get the index of our section within the document
NodeCollection docSections = section.getDocument().getChildNodes(NodeType.SECTION, false);
int sectionIndex = docSections.indexOf(section);
indentAndAppendLine("[Section start] Section index: " + sectionIndex);
mDocTraversalDepth++;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called after all the child nodes of a Section node have been visited.
/// </summary>
public int visitSectionEnd(final Section section) {
mDocTraversalDepth--;
indentAndAppendLine("[Section end]");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Body node is encountered in the document.
/// </summary>
public int visitBodyStart(final Body body) {
int paragraphCount = body.getParagraphs().getCount();
indentAndAppendLine("[Body start] Paragraphs: " + paragraphCount);
mDocTraversalDepth++;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called after all the child nodes of a Body node have been visited.
/// </summary>
public int visitBodyEnd(final Body body) {
mDocTraversalDepth--;
indentAndAppendLine("[Body end]");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Paragraph node is encountered in the document.
/// </summary>
public int visitParagraphStart(final Paragraph paragraph) {
indentAndAppendLine("[Paragraph start]");
mDocTraversalDepth++;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called after all the child nodes of a Paragraph node have been visited.
/// </summary>
public int visitParagraphEnd(final Paragraph paragraph) {
mDocTraversalDepth--;
indentAndAppendLine("[Paragraph end]");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(final Run run) {
indentAndAppendLine("[Run] \"" + run.getText() + "\"");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a SubDocument node is encountered in the document.
/// </summary>
public int visitSubDocument(final SubDocument subDocument) {
indentAndAppendLine("[SubDocument]");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a SubDocument node is encountered in the document.
/// </summary>
public int visitStructuredDocumentTagRangeStart(StructuredDocumentTagRangeStart sdtRangeStart)
{
indentAndAppendLine("[SdtRangeStart]");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a SubDocument node is encountered in the document.
/// </summary>
public int visitStructuredDocumentTagRangeEnd(StructuredDocumentTagRangeEnd sdtRangeEnd)
{
indentAndAppendLine("[SdtRangeEnd]");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Append a line to the StringBuilder and indent it depending on how deep the visitor is into the document tree.
/// </summary>
/// <param name="text"></param>
private void indentAndAppendLine(final String text) {
for (int i = 0; i < mDocTraversalDepth; i++) {
mAcceptingNodeChildTree.append("| ");
}
mAcceptingNodeChildTree.append(text + "\r\n");
}
private int mDocTraversalDepth;
private final StringBuilder mAcceptingNodeChildTree;
}
accept in class Nodevisitor - The visitor that will visit the nodes.DocumentVisitor stopped the operation before visiting all nodes.java.lang.Exception