I’m trying to insert source document into destination document using document builder with ImportFormatMode.USE_DESTINATION_STYLES
option.
But the final document is not generating with expected output.
Files: files.zip (44.8 KB)
Please check following code snippet and let me know what could be the solution to generate final document by using destination document style.
public static void main(String[] args) throws Exception {
Document destDoc = new Document("/home/cipet/Downloads/destDoc.docx");
Document srcDoc = new Document("/home/cipet/Downloads/srcDoc.docx");
DocumentBuilder documentBuilder = new DocumentBuilder(destDoc);
documentBuilder.insertDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES);
documentBuilder.getDocument().save("/home/cipet/Downloads/final.docx");
}
Thanks
@Cipet31490
We have not found any issue with output document. Could you please share the issue detail along with expected output document? We will then provide you more information on it.
image.png (27.0 KB)
Expecting first three(Source doc) line’s style match with remaining(Destination doc) line’s style.
Expected result (Have added expected docx file):
image.png (28.2 KB)
Updated-Files.zip (67.7 KB)
@Cipet31490
Please note that Aspose.Words mimics the behavior of MS Word. If you perform the same scenario using MS Word, you will get the same output.
In your document, the direct font formatting is applied. Please use the following code example to get the desired output.
Document destDoc = new Document(MyDir + "destDoc.docx");
Document srcDoc = new Document(MyDir + "srcDoc.docx");
srcDoc.copyStylesFromTemplate(destDoc);
for (Paragraph para : (Iterable<Paragraph>)srcDoc.getChildNodes(NodeType.PARAGRAPH, true))
{
//para.getParagraphFormat().clearFormatting();
for(Run run : para.getRuns())
{
run.getFont().setName("Arial");
run.getFont().setSize(12.0);
}
para.getParagraphFormat().setStyleName("No Spacing");
}
DocumentBuilder documentBuilder = new DocumentBuilder(destDoc);
documentBuilder.insertDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES);
destDoc.save(MyDir + "21.3.docx");