While Merging Word Documents, Add Page Break before Sections Spanning Across Multiple Pages | Java

Hello,
I am trying to merge multiple documents using aspose words 17.6.
I am using SectionStart.CONTINUOUS & appendDocument() to append the second document’s content to the first document.
But I have a requirement that the document content should not get broken between the pages.For example as stated below.,

  1. when the whole second document’s content could not get fit into the first page then the whole section should be shifted to the next page.(So Result Document with 2 pages)
  2. when the whole second document’s content could be able to get fit into the first page then the second section would stay in the same page.(So the Result document with only one page with 2 sections)

Do we have some option in aspose words, to achieve the above functionality?

Appreciate your help as soon as possible.
Thanks in advance.

@manjusha1236,

Please compress your source Word documents and your expected DOCX files showing the desired behaviors (for both cases) into ZIP format and attach the .zip file here for our reference. You can create these expected files manually by using MS Word. We will then investigate the scenarios on our end and provide you code to achieve the same outputs by using Aspose.Words.

Samples.zip (83.2 KB)
Hello,
Please find the samples as attached.
Kindly, do the needful.

Thanks in advance

@manjusha1236,

You can build logic on the following code to insert Page Break before those Sections which span across multiple pages:

Document doc = new Document("C:\\Temp\\DocContentBreakingBtwPages_AfterMergingInputFiles.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

int index = 0;
for (Section section : doc.getSections()) {
    builder.moveTo(section.getBody().getLastParagraph());

    BookmarkStart bookmarkStart = builder.startBookmark("bookmarked_Scope" + index);
    builder.endBookmark("bookmarked_Scope" + index);
    section.getBody().getFirstParagraph().insertBefore(bookmarkStart, section.getBody().getFirstParagraph().getFirstChild());

    index++;
}

LayoutCollector collector = new LayoutCollector(doc);

for (Bookmark bookmark : doc.getRange().getBookmarks()) {
    if (bookmark.getName().startsWith("bookmarked_Scope")) {
        if (collector.getStartPageIndex(bookmark.getBookmarkStart()) <
                collector.getEndPageIndex(bookmark.getBookmarkEnd())) {
            Section sectionOfThisBookmark = (Section) bookmark.getBookmarkEnd().getAncestor(NodeType.SECTION);
            if (sectionOfThisBookmark.getPreviousSibling() != null) {
                builder.moveTo(((Section) sectionOfThisBookmark.getPreviousSibling()).getBody().getLastParagraph());
                builder.insertBreak(BreakType.PAGE_BREAK);
            }
        }
    }
}

doc.save("C:\\temp\\awjava-21.7.doc");

Hello,

thanks for the prompt response.
this code works well for the documents, where the section content spans into the next page.
But when in case the document is already organised and is not having any content that spans into the next page ., it is throwing some exception.
" java.lang.IllegalArgumentException: Cannot add a node before/after itself."

Can we have a prior check., and then apply the logic accordingly?

Please refer to the “expectedResult” document that I have shared earlier.If I try processing this document which is already organised it is throwing the exception.

Thanks in advance.

@manjusha1236,

We are checking this scenario and will get back to you soon.

@manjusha1236,

Alternatively, you can use LayoutCollector.getNumPagesSpanned method to get the number of pages a Section’s Body spans across. Please check following simplified code:

Document doc = new Document("C:\\Temp\\Samples\\DocContentBreakingBtwPages_AfterMergingInputFiles.doc");
LayoutCollector collector = new LayoutCollector(doc);
DocumentBuilder builder = new DocumentBuilder(doc);

for (Section section : doc.getSections()) {
    // section.getBody().getLastParagraph().getRange().replace(ControlChar.PAGE_BREAK, "");
    if (collector.getNumPagesSpanned(section.getBody()) > 0) {
        if (section.getPreviousSibling() != null) {
            builder.moveTo(((Section) section.getPreviousSibling()).getBody().getLastParagraph());
            builder.insertBreak(BreakType.PAGE_BREAK);
            doc.updatePageLayout();
        }
    }
}

doc.save("C:\\temp\\Samples\\awjava-21.7.doc");

Hello,

I tried with these two approaches.
But I still face some problems with the attached file., the document content is not properly formatted.
Though there is a small paragraph, after a large paragraph., which can fit in the second page., it is still spanning into the next page(you can notice the same in the attached ResultFile).
Can you have a look with the attached files (Source and Result files) ?

Thanks in advance.
files.zip (18.2 KB)

@manjusha1236,

Please also provide your expected DOC file showing the desired output here for our reference. You can create this expected file manually by using MS Word. We will then investigate the scenario further on our end and provide you code to achieve the same output by using Aspose.Words.

Please find the expected sample file for the same resulted file attached beforeExpectedfile.zip (18.0 KB)
.

Thanks in advance.

@manjusha1236,

We have managed to observe the same behavior on our end. We are checking this scenario further and will get back to you soon.

@manjusha1236,

Please check if the following code produces the desired output on your end?

Document doc = new Document("C:\\Temp\\Samples\\sourceFile.doc");

for (int i = 0; i < doc.getSections().getCount(); i++) {
    LayoutCollector collector = new LayoutCollector(doc);
    Section section = doc.getSections().get(i);

    if (collector.getNumPagesSpanned(section.getBody()) > 0) {
        section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        doc.save(outputStream, SaveFormat.DOC);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        doc = new Document(inputStream);
    }
}

doc.save("C:\\temp\\Samples\\awjava-21.8.doc");