public Document execute(Document document)
{
Document zhDoc = this.executeZh();
Document enDoc = this.executeEn();
zhDoc.appendDocument(enDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
document.appendDocument(zhDoc, ImportFormatMode.USE_DESTINATION_STYLES);
int zhDocSectionCount = zhDoc.getSections().getCount();
for (int i = zhDocSectionCount - 1; i >= 0; i--)
{
Section section = (Section)zhDoc.getSections().get(i).deepClone(true);
Section importedSection = (Section)document.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
document.getSections().insert(0, importedSection);
document.getLastSection().remove();
}
i want these 3 document to have their formatting separately and integrate. but my code’s output is the format that ‘document’ has. how could i fix it ?
To append multiple documents while keeping their formatting separate, you should use the ImportFormatMode.KEEP_SOURCE_FORMATTING option when appending each document. This ensures that the formatting of each source document is preserved in the final output.
In your current implementation, you are using ImportFormatMode.USE_DESTINATION_STYLES when appending the zhDoc to the document, which causes the formatting of the destination document to override the source formatting. Instead, you should consistently use ImportFormatMode.KEEP_SOURCE_FORMATTING for all append operations.
Here’s a modified version of your code that should achieve the desired result:
public Document execute(Document document) {
Document zhDoc = this.executeZh();
Document enDoc = this.executeEn();
// Append the English document while keeping its source formatting
zhDoc.appendDocument(enDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Now append the combined document while keeping the source formatting
document.appendDocument(zhDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
int zhDocSectionCount = zhDoc.getSections().getCount();
for (int i = zhDocSectionCount - 1; i >= 0; i--) {
Section section = (Section)zhDoc.getSections().get(i).deepClone(true);
Section importedSection = (Section)document.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
document.getSections().insert(0, importedSection);
document.getLastSection().remove();
}
return document;
}
In this updated code:
Both zhDoc and enDoc are appended using ImportFormatMode.KEEP_SOURCE_FORMATTING, ensuring that their individual formatting is preserved.
The final document also retains the formatting of the appended sections.
This approach should help you integrate the documents while maintaining their distinct formatting.
Document[] files = new Document[]{
new Document("C:\\Temp\\input1.docx"),
new Document("C:\\Temp\\input2.docx"),
new Document("C:\\Temp\\input3.docx"),
new Document("C:\\Temp\\input4.docx"),
new Document("C:\\Temp\\input5.docx")
};
for (Document doc : files)
doc.getFirstSection().getPageSetup().setRestartPageNumbering(true);
Document output = Merger.merge(files, MergeFormatMode.KEEP_SOURCE_LAYOUT);
output.save("C:\\Temp\\out.docx");
If the problem still persists, please attach your input documents and the problematic output.