Losing isLinkedToPrevious() information for Header and Footer

Hi,
I am having the following Issue with Aspose.Words for Java:
When opening a docx file with 2 sections, where the header of section 2 is linked to the header of section 1 and then saving the file as copy, I get a file with an empty header in section 2 instead of the linked content of header 1.

The following code example demonstrates the issue (executed with version 23.5):

package com.sample;

import com.aspose.words.Document;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.Section;
import org.junit.jupiter.api.Test;

public class LinkedHeaderTest {

    @Test
    void headerOfSection2ShouldBeLinkedToHeaderOfSection1() throws Exception {
        Document document = new Document("C:\\work\\projects\\various\\aspose_words\\src\\test\\resources\\linked.docx");
        int i = 0;
        for (Section section : document.getSections()) {
            System.out.println("############ Section " + ++i);
            HeaderFooter headerPrimary = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
            System.out.println("HEADER_PRIMARY: isLinkedToPrevious: " + headerPrimary.isLinkedToPrevious());
            System.out.println("content: " + headerPrimary.getFirstParagraph().getText());
        }
        document.save("C:\\work\\projects\\various\\aspose_words\\src\\test\\resources\\linked_copy.docx");
    }

}

This produces the following output, that also shows, that the information that the header of section 2 is linked to the header of section 1 is not present:

############ Section 1
HEADER_PRIMARY: isLinkedToPrevious: false
content: Header for Section 1 and Section 2
############ Section 2
HEADER_PRIMARY: isLinkedToPrevious: false
content: 

Please find this sample attached as .zip aspose_words.zip (41.1 KB)

@alfred.schalk In MS Word document header/footer is also linked to previous when header/footer is not present. You should modify your code like this:

Document document = new Document("C:\\Temp\\in.docx");
int i = 0;
for (Section section : document.getSections()) {
    System.out.println("############ Section " + ++i);
    HeaderFooter headerPrimary = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
    if(headerPrimary == null) {
        System.out.println("HEADER_PRIMARY: isLinkedToPrevious: true");
    }
    else {
        System.out.println("HEADER_PRIMARY: isLinkedToPrevious: " + headerPrimary.isLinkedToPrevious());
        System.out.println("content: " + headerPrimary.getFirstParagraph().getText());
    }
}
document.save("C:\\Temp\\out.docx");

But since you are running the test in evaluation mode, Aspose.Words inserts an evaluation watermark, which is inserted into the header. That is why in evaluation mode primary header is not null in the second section.

1 Like

Thanks for your answer, @alexey.noskov
I was not aware that the evaluation watermark was part of the header because I only noticed the evaluation text in the footer.
You are right. With applied license the header of the second section is null.
Might there be cases where headerPrimary is not null for linked to previous headers and I would need to check headerPrimary.isLinkedToPrevious() additionally? Or can I rely on the object being null?

@alfred.schalk Usually, header/footer linked to previous is null. But both cases are valid, section can have header, which is explicitly linked to previous. In this case header/footer is not null and isLinkedToPrevious returns true. For example, this can occur if you create header/footer programmatically and set isLinkedToPrevious flag.

1 Like