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 Jan 15, 2024 4:00 pm

Hello Team,

I'm trying to merge document with Spire Doc for Java (12.1.0) on Windows environnement

Documents are merge but the "Styles" are modified.
To be more explicite, in the first document (doc1.docx) Style named "normal" use format -> paragraphe -> spacing -> line spacing : "Multiple"
The second one (doc2.docx) use "Single" for the same property.

You can see on the result file (merge.docx) that they are 2 styles named: normal and normal-uuid, both are using "Multiple" for the previous property.

I have try with these 2 methodes to merge documents :

Code: Select all
        // documents as List of Document
        Document mergeDocument = documents.get(0);

        for (int i = 1; i < documents.size(); i++) {

            Paragraph paragraph = mergeDocument.getLastParagraph();
            paragraph.insertSectionBreak(SectionBreakType.No_Break);

            Document document = documents.get(i);

            // Add in new page the document not directly after the last paragraph
            mergeDocument.importContent(document, true); // true to import Style
        }
        mergeDocument.saveToStream(os, mergeDocument.getOriginalFileFormat());


Code: Select all
        // documents as List of Document
        Document mergeDocument = documents.get(0);

        for (int i = 1; i < documents.size(); i++) {

            Paragraph paragraph = mergeDocument.getLastParagraph();
            paragraph.insertSectionBreak(SectionBreakType.No_Break);

            Document document = documents.get(i);

            //Loop through the second document to get all the sections
            for (Object section : document.getSections()) {
                Section sec = (Section) section;
                //Loop through the sections of the second document to get their child objects
                for (Object docObj : sec.getBody().getChildObjects()) {
                    DocumentObject childObj = (DocumentObject) docObj;

                    //Get the last section of the first document
                    Section lastSection = mergeDocument.getLastSection();

                    //Add the child objects to the last section of the first document
                    Body body = lastSection.getBody();
                    body.getChildObjects().add(childObj.deepClone());
                }
            }
        }
        mergeDocument.saveToStream(os, mergeDocument.getOriginalFileFormat());


Have you ever encountered this problem?

Relin579
 
Posts: 8
Joined: Mon Jan 15, 2024 8:57 am

Tue Jan 16, 2024 1:53 am

Hello,

Thank you for your inquiry.
After thoroughly testing your code, we have discovered a solution to the problem you encountered. By adding the line "document.setKeepSameFormat(true)" at the appropriate location in your code, the issue can be resolved. Please review the specific placement of this code snippet provided below:
Code: Select all
...
Paragraph paragraph = mergeDocument.getLastParagraph();
paragraph.insertSectionBreak(SectionBreakType.No_Break);
Document document = documents.get(i);
// Set the flag to keep the same format as the original document
document.setKeepSameFormat(true);
...

Adding the "document.setKeepSameFormat(true)" line ensures that the formatting of the original document is preserved during the conversion process.
Additionally, regarding your query about merging Word documents, there is an alternative method available. You can utilize the following approach:
Code: Select all
// Get the first document from the list
Document mergeDocument = documents.get(0);
// Iterate through the remaining documents in the list
for (int i = 1; i < documents.size(); i++) {
    Document document = documents.get(i);
    // Set the flag to keep the same format as the original document
    document.setKeepSameFormat(true);
    // Iterate through the sections of the current document
    for (int j = 0; j < document.getSections().getCount(); j++) {
        // Deep clone each section and add it to the merge document
        mergeDocument.getSections().add(document.getSections().get(j).deepClone());
    }
}
// Set the section break code to "No_Break" for each section in the merge document
for (int i = 0; i < mergeDocument.getSections().getCount(); i++) {
    Section section = mergeDocument.getSections().get(i);
    section.setBreakCode(SectionBreakType.No_Break);
}
// Save the merge document to a stream with the original file format
mergeDocument.saveToStream(os, mergeDocument.getOriginalFileFormat());

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Tue Jan 16, 2024 10:02 am

Hello,

Thank you for the fast answer.

Your solution work like attended, thank again.

Relin579
 
Posts: 8
Joined: Mon Jan 15, 2024 8:57 am

Tue Jan 16, 2024 10:10 am

Hello,

You're welcome.
If you encounter other issues related to our products in the future, please feel free to contact us.
Have a nice day.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc