How can i append multiple document but keep their formatting separately ?`

in the following code :

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 ?

@Madecho

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.

and when i switch the order of appending.

  zhDoc.appendDocument(enDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
  zhDoc.appendDocument(document, ImportFormatMode.KEEP_SOURCE_FORMATTING);

the zhDoc and enDoc appear good. however ‘document’ has something wrong like this :


it lost many formatting style that it used to have

@Madecho You can try using Merger class to merge documents and specify MergeFormatMode.KEEP_SOURCE_LAYOUT.

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.

1 Like

test.zip (82.5 KB)
@alexey.noskov
hi alexey, here’s test input and output document with the test java code
as you can see in the ‘result.docx’


the header is wrong. how can i fix that ?

@Madecho Please try using MergeFormatMode.MERGE_FORMATTING:

LoadOptions loadOptions = new LoadOptions();
loadOptions.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.CHINESE_PRC);
Document cover = new Document("C:\\Temp\\cover.docx", loadOptions);
Document content = new Document("C:\\Temp\\cotent.docx", loadOptions);
    
Document[] documents = { cover, content };
    
Document output = Merger.merge(documents, MergeFormatMode.MERGE_FORMATTING);
    
content.updateFields();
output.save("C:\\Temp\\result.docx");

result.docx (29.7 KB)

1 Like