We're sorry Aspose doesn't work properply without JavaScript enabled.

# A question about the table

I have a requirement that I need to reformat a document. So I create a new document, then read the contents of the old document and write them into the new document. The general framework is as follows.

Document document = new Document(filePath);
for (Section section : document.getSections()) {
for (Paragraph paragraph: section.getBody().getParagraphs()) {
write;
}
}


I don’t know how to read the old document table and then write it to the same location as the new document. I hope it can help me.

@wslioy The best way t achieve what you need is using DocumentVisitor. Please see our documentation for more information:
https://docs.aspose.com/words/java/extract-selected-content-between-nodes/#extract-content-using-documentvisitor

I see what you mean. However, due to the historical code, it is not convenient to use DocumentVisitor to implement this requirement. I would like to ask if there is a way to insert the Table of a document into a new document. Since it’s a different document and I can’t insert the node directly, I can’t find the method.

@wslioy If you cannot use DocumentVisitor approach, you should lop through all document’s children, not only paragraphs:

for (Section section : document.getSections())
{
for (Node child : section.getBody().getChildNodes().toArray())
{
if (child.getNodeType() == NodeType.PARAGRAPH)
{
// Write a paragraph.
}
else if (child.getNodeType() == NodeType.TABLE)
{
// Write a table.
}
// There might be more conditions here.

}
}


When you need to copy a node from one document into another, you essentially need to import the nodes of the source document into the destination one using the ImportNode method. After importing your nodes, you can insert them into another document.

I used the DocumentBuilder to build my document and used the importNode method to import the node according to your suggestion. The approximate method is as follows, but I still can’t insert the node, I was prompted that the node was generated by a different document.

Document document = new Document(filePath);
Document newDocument = new Document();
DocumentBuilder builder = new DocumentBuilder(newDocument);
Table table = (Table) document.getChild(NodeType.TABLE, index, true);
builder.getDocument().importNode(table, true);
builder.insertNode(table);


I can’t just insert it like that?

@wslioy First of all you should insert the imported node, in your code you insert the table from another document. Also, to insert a table you should use insertBefore or insertAfter methods:

Document document = new Document("C:\\Temp\\in.docx");
Document newDocument = new Document();
DocumentBuilder builder = new DocumentBuilder(newDocument);
Table table = (Table) document.getChild(NodeType.TABLE, index, true);
Node importedNode = builder.getDocument().importNode(table, true);
builder.getCurrentParagraph().getParentNode().insertBefore(importedNode, builder.getCurrentParagraph());
newDocument.save("C:\\Temp\\out.docx");


OK, I know how to do it. Thank you very much for your answer.

1 Like