I am attempting to merge three DOCX files (0.docx, 1.docx, 2.docx) into a single document using spire.doc 12.9.0. However, I am encountering an issue where the styles of the third file (2.docx) are being overridden by the styles of the second file (1.docx) in the merged document.
I would like each file's original formatting and styles to remain intact within the final merged document, but the third file seems to inherit the styling of the second file. I have attached the relevant DOCX files (0.docx, 1.docx, 2.docx) and the output file (MergedDocument.docx) for reference.
Could anyone suggest how I can preserve the individual styles of each document in the merged output?
Thank you in advance for your help.
- Code: Select all
<properties>
<java.version>17</java.version>
</properties>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>12.9.0</version>
</dependency>
public static void main(String[] args) {
List<String> filePathList = new ArrayList<>();
filePathList.add("templates/0.docx");
filePathList.add("templates/2.docx");
filePathList.add("templates/3.docx");
Document mainDoc = new Document(filePathList.get(0),FileFormat.Docx_2013);
mainDoc.getLastSection().addParagraph().appendBreak(BreakType.Page_Break);
for (int cntDoc = 0; cntDoc < filePathList.size(); cntDoc++) {
if (cntDoc != 0) {
Document subDoc = new Document(filePathList.get(cntDoc));
for (Section section : (Iterable<Section>) subDoc.getSections()) {
Section newSection = section.deepClone();
mainDoc.getSections().add(newSection);
}
}
mainDoc.getLastSection().getHeadersFooters().setLinkToPrevious(false);
}
mainDoc.saveToFile("templates/MergedDocument.docx", FileFormat.Docx_2019);
}