Append word styles problem

I had met a problem about styles after append document.

The code:

String path = "C:\\Users\\z_jia\\Desktop\\a\\MobileH5\\";
Document word = new Document(path + "\\1.docx");
Document coverWord = new Document(path + "\\cover.docx");
word.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
var t = (Document) word.deepClone(false);
t.appendDocument(coverWord, ImportFormatMode.USE_DESTINATION_STYLES);
t.appendDocument(word, ImportFormatMode.USE_DESTINATION_STYLES);
word = t;

word.save(path + "//result.docx");

1.docx (492.9 KB)

cover.docx (19.5 KB)

result.docx (496.5 KB)

@GusGus

What is happening

ImportFormatMode.USE_DESTINATION_STYLES tells Aspose.Words to convert every style from the source document to the matching style that already exists in the destination document.
If the destination already contains a style with the same name but a different definition, the source formatting will be lost – the text will be rendered with the destination’s version of that style.

In your code you:

t.appendDocument(coverWord, ImportFormatMode.USE_DESTINATION_STYLES);
t.appendDocument(word,      ImportFormatMode.USE_DESTINATION_STYLES);

so the cover document and the original document are both forced to use the styles that belong to t (the cloned 1.docx).
Consequently the final result.docx shows the styling of cover.docx (or 1.docx) incorrectly.


How to keep the original formatting

1. Use KEEP_SOURCE_FORMATTING

If you want the appended documents to retain their own style definitions, use the default import mode:

t.appendDocument(coverWord, ImportFormatMode.KEEP_SOURCE_FORMATTING);
t.appendDocument(word,      ImportFormatMode.KEEP_SOURCE_FORMATTING);

KEEP_SOURCE_FORMATTING copies the source styles into the destination document (creating new style IDs when there is a name clash). The visual appearance of each part remains exactly as in the original files.

2. (Optional) Remove the unnecessary deep‑clone

deepClone(false) creates a shallow copy of the original document – it does not copy the underlying style definitions, which can also cause style‑loss issues.
You can:

  • Eliminate the clone entirely, or
  • Clone with deepClone(true) if you really need a separate instance.
// No cloning needed – just work with a fresh Document instance:
Document result = new Document(path + "1.docx");

// Append the cover first (keep its original styles)
result.appendDocument(new Document(path + "cover.docx"),
                      ImportFormatMode.KEEP_SOURCE_FORMATTING);

// Append the main body (keep its original styles as well)
result.appendDocument(new Document(path + "1.docx"),
                      ImportFormatMode.KEEP_SOURCE_FORMATTING);

3. When you must use destination styles for the main body

If the requirement is to keep the cover styling untouched but force the rest of the document to adopt the destination’s styles, combine the two modes:

// 1️⃣ Append cover – keep its own styles
result.appendDocument(new Document(path + "cover.docx"),
                      ImportFormatMode.KEEP_SOURCE_FORMATTING);

// 2️⃣ Append the main document – map its styles to the destination
result.appendDocument(new Document(path + "1.docx"),
                      ImportFormatMode.USE_DESTINATION_STYLES);

Full working example (Java)

import com.aspose.words.*;

public class AppendWithStyles {
    public static void main(String[] args) throws Exception {
        String folder = "C:\\Users\\z_jia\\Desktop\\a\\MobileH5\\";

        // Load documents
        Document coverDoc = new Document(folder + "cover.docx");
        Document bodyDoc  = new Document(folder + "1.docx");

        // Create the result document (empty or a copy of the body)
        Document result = new Document();            // empty document
        // Alternatively: Document result = (Document)bodyDoc.deepClone(true);

        // Append cover – keep its original formatting
        result.appendDocument(coverDoc,
                ImportFormatMode.KEEP_SOURCE_FORMATTING);

        // Append the main content – map its styles to the result document
        result.appendDocument(bodyDoc,
                ImportFormatMode.USE_DESTINATION_STYLES);

        // Save
        result.save(folder + "result.docx");
    }
}

References

If you have any further questions (e.g., dealing with specific style name clashes or merging custom style definitions), feel free to reply with the details and I’ll be happy to help!

@GusGus The problem is not reproducible on my side using the latest 25.11 version of Aspose.Words. Here is the output produced on my side:
out.docx (496.3 KB)

As I can see you are using 24.8 version. Please try using the latest version and let us know if the problem still persists on your side.

May I apply temp license for testing?

@GusGus Sure, if you would like to test new version of Aspose.Words without evaluation version limitations, you can request a free 30-days temporary license .