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

Free Support Forum - aspose.com

Issue with insertDocument and html table

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();

}

}

}

-----

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think it is better to use InsertHtml method to insert HTML into the document. Please see the following link for more information.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/com/aspose/words/documentbuilder.html#insertHtml(java.lang.String)

Also, please attach your document for testing.

Best regards.

Thank you for the suggestion, I will test it.



Apologies for not attaching the docs. Forgot it…



mainDoc.dot = main with bookmark

quotation.html = original html doc

quotation.html.doc = html doc written as word doc

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Please try using the following code:

// Open the document and create DocumentBuilder.

Document doc = new Document("C:\\Temp\\mainDoc.dot");

DocumentBuilder builder = new DocumentBuilder(doc);

//Read HTML from file

File file = new File("C:\\Temp\\quotation.html");

StringBuffer contents = new StringBuffer();

BufferedReader reader = null;

String html = "";

try

{

reader = new BufferedReader(new FileReader(file));

String text = null;

// repeat until all lines is read

while ((text = reader.readLine()) != null)

{

contents.append(text).append(System.getProperty("line.separator"));

}

html = contents.toString();

}

catch (Exception e)

{

html = "";

}

finally

{

reader.close();

}

//move DocumentBuilder cursor to bookmark

builder.moveToBookmark("LetRules");

//Insert HTML

builder.insertHtml(html);

// Save the modified document.

doc.save("C:\\Temp\\out.doc");

Best regards,