Append Word Documents | Retain 'Automatically Resize to Fit Contents' Table Setting | DOCX to PDF Table Resize Java

Table is losing style when we append the documentsResize.zip (86.5 KB)
. When we debug found that ‘Automatically resize to fit contents’ is causing the issue. Could you please help. Attaching input DOCX and oputput DOCX and PDF documents.

Document merged = Document();
Document doc = new Document("\INPUT.docx");
merged.appendDocument(doc,ImportFormatMode.KEEP_SOURCE_FORMATTING);
merged.save("\OUT.docx", SaveFormat.DOCX);
merged.save("\OUT.pdf", SaveFormat.PDF);

@vijji,

You can workaround this problem by using the following Java code of Aspose.Words:

Document doc = new Document("C:\\Temp\\Resize\\input.docx");
Document merged = (Document) doc.deepClone(false);
merged.ensureMinimum();
merged.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
merged.save("C:\\Temp\\Resize\\20.8.docx");
merged.save("C:\\Temp\\Resize\\20.8.pdf");

Thank you for the solution.

If I need to append another document with similar conetnt (Automatically resize to fit contents), can I use below code?. Solution is working but wanted to check with you, if it is the rightway of doing.

Also my understanding is that deepClone will copy the document and this is useful when we wanted to generate more than one copy. appendDocuemnt will append the docuemnt to the last section of the document. Could you kindly explain how these two methods are working together in your solution. Thank you.

Document doc = new Document(“C:\Temp\Resize\input.docx”);
Document doc2 = new Document(“C:\Temp\Resize\input2.docx”);

Document merged = (Document) doc.deepClone(false);
merged.ensureMinimum();
merged = (Document) doc2 .deepClone(false);

merged.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
merged.appendDocument(doc2,ImportFormatMode.KEEP_SOURCE_FORMATTING);

merged.save(“C:\Temp\Resize\20.8.docx”);
merged.save(“C:\Temp\Resize\20.8.pdf”);

@vijji,

The Document.deepClone method will perform a deep copy of the Document. This will copy all the Styles, Page Setup information such as Page Width / Height, orientation and many other attributes into the newly created Document instance. But to simulate Document merged = Document();, we needed this copy of the Document as blank that is why we are passing false to deepClone. The Document.ensureMinimum method creates one Section with one Paragraph in this new Document. The workaround was to ensure that we have all the required Styles and PageSetup information readily available in new Document in which we are appending other documents to. Please let us know if you have any troubles and we will be glad to look into this further for you.

Thank you. Could you please confirm the approach I used for appending more than one document.

@vijji,

The following code should work fine:

Document doc = new Document("C:\\Temp\\Resize\\input.docx");
Document doc2 = new Document("C:\\Temp\\Resize\\input2.docx");
Document doc3 = new Document("C:\\Temp\\Resize\\input3.docx");
Document doc4 = new Document("C:\\Temp\\Resize\\input4.docx");

doc.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc.appendDocument(doc3, ImportFormatMode.KEEP_SOURCE_FORMATTING);
doc.appendDocument(doc4, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc.save("C:\\Temp\\Resiz\\20.8.docx");
doc.save("C:\\Temp\\Resize\\20.8.pdf");

This solution is not working when we have table with ‘‘Automatically resize to fit contents’’, your deepclone() solution is working fine. My question is when we wanted to append multiple documents, can we use deepclone() multiple times like below. Solution is working finr but wanted to check with you if it is the rightway. Thank you.

Document doc = new Document(“C:\Temp\Resize\input.docx”);
Document doc2 = new Document(“C:\Temp\Resize\input2.docx”);

Document merged = (Document) doc.deepClone(false);
merged.ensureMinimum();
merged = (Document) doc2 .deepClone(false);

merged.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
merged.appendDocument(doc2,ImportFormatMode.KEEP_SOURCE_FORMATTING);

merged.save(“C:\Temp\Resize\20.8.docx”);
merged.save(“C:\Temp\Resize\20.8.pdf”);

@vijji,

Please ZIP and attach your simplified input.docx, input2.docx, 20.8.docx documents and a screenshot highlighting the problematic area in 20.8.docx here for further testing. We will then investigate the outputs produced against both cases (with and without using deepClone) on our end and provide you more information.