Extra empty paragraph is added in the header after joining documents using Java

Hi,

I’m trying to merge two documents using appendDocument method and I’m facing an issue with header/footer sizes.

Main document has the same header for all pages.
Second document has a different first page header and footer.

I’ve also set the linkToPrevious property to false:
secondDocument.getFirstSection().getHeadersFooters().linkToPrevious(false);

And I’m appending with ImportFormatMode.KEEP_DIFFERENT_STYLES:
document.appendDocument(secondDocument, ImportFormatMode.KEEP_DIFFERENT_STYLES);

Combined document has a larger empty space at the begining of the last two pages:

image.png (7.0 KB)
image.png (10.5 KB)

docs.zip (91.6 KB)

Is there anyway I can fix this?

Thanks

@mrastenis

Please note that Aspose.Words mimics the behavior of MS Word. If you join the documents using the MS Word, you will get the same output.

We have tested the scenario using the latest version of Aspose.Words for Java 20.5 and have not found the shared issue. So, please use Aspose.Words for Java 20.5.

Thanks for a quick reply.

I’ve tried upgrading to 20.5 version and I’m still facing this issue.

I’m using the following code:

try {
Document doc1 = new Document(new FileInputStream(new File(“path/to/doc1.docx”)));
Document doc2 = new Document(new FileInputStream(new File(“path/to/doc2.docx”)));

  doc2.getFirstSection().getHeadersFooters().linkToPrevious(false);
  doc1.appendDocument(doc2, ImportFormatMode.KEEP_DIFFERENT_STYLES);

  doc1.save("path/to/result.docx");
} catch (Exception e){}

Last page of combined document contains an empty header/footer whereas the “doc2.docx” doesn’t:
image.png (6.4 KB)

@mrastenis

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-20474 . You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@mrastenis

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-20474) as ‘Not a Bug’.

It is a well-known MS Word feature. That is how linking headers footers to the previous section works. When a section has no header/footer, it uses a header/footer from the previous section automatically. When a section has its own header/footer, it uses it.

To unlink header/footer from the previous section (which means header/footer is empty), we must add something to it. MS Word adds one empty paragraph, as well as Aspose.Wrods.

To workaround this issue, you can reduce the size of paragraph break. Please check the following code example.

Document doc1 = new Document(MyDir + "doc1.docx");
Document doc2 = new Document(MyDir + "doc2.docx");

doc2.getFirstSection().getHeadersFooters().linkToPrevious(false);
MakeUnlinkedHeadersFootersSmall(doc2.getFirstSection().getHeadersFooters());
doc1.appendDocument(doc2, ImportFormatMode.USE_DESTINATION_STYLES);
doc1.save(MyDir + "20.6.docx"); 

private static void MakeUnlinkedHeadersFootersSmall(HeaderFooterCollection headersFooters)
{
    for (HeaderFooter headerFooter : headersFooters)
    {
        if ((headerFooter.getParagraphs().getCount() == 1) && (!headerFooter.getFirstParagraph().hasChildNodes()))
            headerFooter.getFirstParagraph().getParagraphBreakFont().setSize(0);
    }
}