Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Oct 14, 2024 7:37 am

Hello everyone,

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);
    }

damy_ben
 
Posts: 4
Joined: Mon Oct 14, 2024 5:00 am

Mon Oct 14, 2024 10:24 am

Hello,

Thanks for your inquiry. I have reviewed the document in your attachment and found that 0.docx and 2.docx were created by the Word 2018 version, while 3.docx was created by the Word 2010 version. This results in different compatible layouts between them, causing the style of the third file to be overwritten by the style of the second file. Even when merging in Microsoft Word, it is impossible to maintain the original layout of all documents. To maintain the same style, it is necessary to maintain the same compatible layout. Thanks for your understanding.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 306
Joined: Mon Jul 15, 2024 5:40 am

Mon Oct 14, 2024 12:36 pm

Hi Amin,

Thank you for your prompt response. Given the situation, is there a recommended solution to ensure that all input Word files are converted to the same version before merging them?

Additionally, during my evaluation of another solution, I came across a feature called ImportFormatMode.KEEP_SOURCE_FORMATTING, which worked quite effectively. Does Spire.Doc offer a similar feature for preserving the original formatting during document import?

Thank you for your assistance.

Thanks,
Damy.

damy_ben
 
Posts: 4
Joined: Mon Oct 14, 2024 5:00 am

Tue Oct 15, 2024 3:07 am

Hello,

Thanks for your feedback. For this situation, you can refer to the following code to set the same format when processing documents. If you have any other questions, please feel free to write to me.
Code: Select all
  List<String> filePathList = new ArrayList<>();
       
        filePathList.add("0.docx");
        filePathList.add("2.docx");
        filePathList.add("3.docx");

        Document mainDoc = new Document(filePathList.get(0), FileFormat.Docx_2013);
        mainDoc.getLastSection().addParagraph().appendBreak(BreakType.Page_Break);
       // Set to maintain source format
        mainDoc.setKeepSameFormat(true);
        for (int cntDoc = 0; cntDoc < filePathList.size(); cntDoc++) {
            if (cntDoc != 0) {
                Document subDoc = new Document(filePathList.get(cntDoc));
                // Copy Style
                subDoc.cloneDefaultStyleTo(mainDoc);
                for (Section section : (Iterable<Section>) subDoc.getSections()) {
                    Section newSection = section.deepClone();
                    mainDoc.getSections().add(newSection);
                    if (cntDoc==1){
                        mainDoc.getLastSection().addParagraph().appendBreak(BreakType.Page_Break);
                    }
                }
            }
        }
        mainDoc.saveToFile("result.docx", FileFormat.Docx_2019);

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 306
Joined: Mon Jul 15, 2024 5:40 am

Tue Oct 15, 2024 12:41 pm

a Big thank you for the solution you provided. It worked as expected with only a minor adjustment to the code. I truly appreciate your support and assistance.

Code: Select all
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);
mainDoc.getLastSection().addParagraph().appendBreak(BreakType.Page_Break);
mainDoc.setKeepSameFormat(true);
for (int cntDoc = 0; cntDoc < filePathList.size(); cntDoc++) {
   if (cntDoc != 0) {
      Document subDoc = new Document(filePathList.get(cntDoc));
      // Keep format in sub documents too
      subDoc.setKeepSameFormat(true);
      subDoc.cloneDefaultStyleTo(mainDoc);
      for (Section section : (Iterable<Section>) subDoc.getSections()) {
         Section newSection = section.deepClone();
         mainDoc.getSections().add(newSection);
         if (cntDoc==1){
            mainDoc.getLastSection().addParagraph().appendBreak(BreakType.Page_Break);
         }
      }
   }
}
mainDoc.saveToFile("templates/result.docx", FileFormat.Docx);



Thanks
Regrads
Damy

damy_ben
 
Posts: 4
Joined: Mon Oct 14, 2024 5:00 am

Wed Oct 16, 2024 1:14 am

Hello,

Thanks for your reply.
I'm glad to hear this news. If you have any other questions, please feel free to write to me.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 306
Joined: Mon Jul 15, 2024 5:40 am

Wed Oct 16, 2024 5:56 am

I have encountered another minor issue with the merged document. It appears that the header settings from the original document have been altered, resulting in a discrepancy in the document length (number of pages). Could you please advise if there is a solution to address this issue?

damy_ben
 
Posts: 4
Joined: Mon Oct 14, 2024 5:00 am

Wed Oct 16, 2024 8:10 am

Hello,

Thanks for your feedback. I changed your code and the test results are all normal. You can refer to the following code, and if you have any further questions, feel free to provide feedback at any time.
Code: Select all
 List<String> filePathList = new ArrayList<>();
   
        filePathList.add("0.docx");
        filePathList.add("2.docx");
        filePathList.add("3.docx");

        Document mainDoc = new Document(filePathList.get(0), FileFormat.Docx_2013);
        mainDoc.setKeepSameFormat(true);
        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));
               // subDoc.cloneDefaultStyleTo(mainDoc);
                subDoc.setKeepSameFormat(true);
                for (Section section : (Iterable<Section>) subDoc.getSections()) {
                    Section newSection = section.deepClone();
                    mainDoc.getSections().add(newSection);
                }
            }
        }
        mainDoc.saveToFile("result.docx", FileFormat.Docx_2019);

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 306
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.Doc