When I take a word document the below method works perfect to insert a(ny) Word document.
However, when I take a html document to insert it it does not work well. To be more specific, the document is inserted but a table is not
I am sure I miss the obvious but I don’t know how to do this.
Also, when I take the html document and write it as a .doc it goes wrong.
The script is based on an earlier provided sample:
-----
private void insertDocument(final com.aspose.words.Document srcDoc) throws Exception {
builder.writeln();
// Content of srcdoc will be inserted after this node
Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling();
// Content of first paragraph of srcDoc will be apended to this parafraph
final Paragraph insertAfterParagraph = (Paragraph)insertAfterNode;
// Content of last paragraph of srcDoc will be apended to this parafraph
final Paragraph insertBeforeParagraph = builder.getCurrentParagraph();
// We will be inserting into the parent of the destination paragraph.
final CompositeNode dstStory = insertAfterNode.getParentNode();
// Create temporary list
List srcTmpList = null;
List dstTmpList = null;
// Remove empty paragraphs from the end of document
while (!srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes())
{
srcDoc.getLastSection().getBody().getLastParagraph().remove();
}
// Loop through all sections in the source document.
final int sectCount = srcDoc.getSections().getCount();
for (int sectIndex = 0; sectIndex
final Section srcSection = srcDoc.getSections().get(sectIndex);
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
final int nodeCount = srcSection.getBody().getChildNodes().getCount();
for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
{
final Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);
// Do not insert node if it is a last empty paragarph in the section.
final Paragraph para = (Paragraph)srcNode;
if ((para != null) && para.isEndOfSection() && (!para.hasChildNodes()))
{
break;
}
// If current paragraph is first paragraph of srcDoc
// then appent its content to insertAfterParagraph
if (para.equals(srcDoc.getFirstSection().getBody().getFirstParagraph()) && insertAfterParagraph.hasChildNodes())
{
final int firstParaChildCount = para.getChildNodes().getCount();
for (int childIndex = 0; childIndex < firstParaChildCount; childIndex++)
{
final Node node = para.getChildNodes().get(childIndex);
final Node dstNode = doc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
insertAfterParagraph.appendChild(dstNode);
}
// If subdocument contains only one paragraph
// then copy content of insertBeforeParagraph to insertAfterParagraph
// and remove insertBeforeParagraph
if (srcDoc.getFirstSection().getBody().getFirstParagraph().equals(srcDoc.getLastSection().getBody().getLastParagraph()))
{
while (insertBeforeParagraph.hasChildNodes())
{
insertAfterParagraph.appendChild(insertBeforeParagraph.getFirstChild());
}
insertBeforeParagraph.remove();
}
}
// If current paragraph is last paragraph of srcDoc
// then appent its content to insertBeforeParagraph
else if (para.equals(srcDoc.getLastSection().getBody().getLastParagraph()) && insertBeforeParagraph.hasChildNodes())
{
Node previouseNode = null;
final int firstParaChildCount = para.getChildNodes().getCount();
for (int childIndex = 0; childIndex < firstParaChildCount; childIndex++)
{
final Node node = para.getChildNodes().get(childIndex);
final Node dstNode = doc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
if (previouseNode == null)
{
insertBeforeParagraph.insertBefore(dstNode, insertBeforeParagraph.getFirstChild());
}
else
{
insertBeforeParagraph.insertAfter(dstNode, previouseNode);
}
previouseNode = dstNode;
}
}
else
{
// This creates a clone of the node, suitable for insertion into the destination document.
final Node newNode = doc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
if (srcNode.getNodeType() == NodeType.PARAGRAPH)
{
final Paragraph srcTmpPar = (Paragraph)srcNode;
final Paragraph dstTmpPar = (Paragraph)newNode;
if (srcTmpPar.getListFormat().getList() == srcTmpList)
{
dstTmpPar.getListFormat().setList(dstTmpList);
}
else
{
srcTmpList = srcTmpPar.getListFormat().getList();
dstTmpList = dstTmpPar.getListFormat().getList();
}
}
// Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
if (!insertAfterParagraph.hasChildNodes())
{
insertAfterParagraph.remove();
}
if (!insertBeforeParagraph.hasChildNodes())
{
insertBeforeParagraph.remove();
}
}
-----