@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!