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

Free Support Forum - aspose.com

DocumentBuilder insertHtml method change the formatting of the document

Hi,
I am using Aspose.Words for Java 13.12.0.0. I am facing a issue with the builder object insertHtml method. When i insert a HTML in the document the formatting of some portion of the document gets changed. I can not figure it out why this is happening. Can some one help me please. i attached the code snippet and the original document and the output document as well.

NOTE: Attached files are generated with trial version of aspose but i tried with full version also but the result is same.

public static void main(String[] args) throws Exception {
Document document = new Document(“D:\Supplier Template.docx”);
String headerFile = “D:\Header.doc”;
String metaDataHTML = “”;
metaDataHTML = getHtmlFromDoc(headerFile);
metaDataHTML = metaDataHTML.replaceAll("<div", “<p”);
metaDataHTML = metaDataHTML.replaceAll("", “

”);
metaDataHTML = metaDataHTML.replaceAll("


", "

 

“);
DocumentBuilder db = new DocumentBuilder(document);
db.insertHtml(metaDataHTML);
document.save(“D:\Supplier Template output.docx”);
}

public static String getHtmlFromDoc(String fileName) throws Exception {
Document doc;
String htmlText = “”;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();

try
{
doc = new Document(fileName);

HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.HTML);
saveOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE);

doc.save(outStream, saveOptions);
htmlText = outStream.toString(“UTF-8”);

// Extracting only text between body tags
htmlText = htmlText.substring(htmlText.indexOf(”

") + “
”.length(),
htmlText.indexOf("
"));
}
catch (Exception e)
{

}

return htmlText;
}
Hi Reetam,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for Java 16.1.0 and have not found the shared issue. We have attached the output document with this post for your kind reference. Please check this output document and share which portions of the document are changed.

We noticed that you are inserting the contents of Header.doc at the start of output document. We suggest you please use following code example to achieve your requirements and use latest version of Aspose.Words for .NET 16.1.0. Hope this helps you.

Document document = new Document(MyDir + "Supplier+Template.docx");

DocumentBuilder builder = new DocumentBuilder(document);

builder.moveToDocumentStart();

builder.insertDocument(new Document(MyDir + "Header.doc"), ImportFormatMode.KEEP_SOURCE_FORMATTING);

document.save(MyDir + "Out.docx");

Hi Tahir,
Thank you for the solution. After updating to the version 15.12 the issue has been solve. We can not use the latest version now so we are using the latest -1 and also i can not use the insert document because by converting document to html we perform some operations.

Hi Reetam,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.