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?