We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

User defined footers are getting deleted from document with multiple section breaks

Hi,

I am facing one issue and need help in understanding if I am doing something wrong.

Requirement : Add a logo/image to the footer of each page of the document. (A document can have multiple section breaks)

Problem/Retro Steps :

  1. Upload a document which has multiple section breaks and user-defined footers on each page.

  2. My code will parse the document and add image/logo on each page of the document.

  3. Unfortunately, the code is somehow deleting the user-defined footers from some of the pages of the document.

Questions :

What is wrong with the current code?

My code is adding logo/image perfectly fine on each page of the document. But somehow, it is removing user-defined footers from some pages of the document (when document has multiple section breaks).

An Example Document for reference : Document With Multiple Section breaks

Code :

//wordBytes --> bytes of the word document uploaded.
ByteArrayInputStream wordDocInputStream = new ByteArrayInputStream(wordBytes);
Document wordDoc = new Document(wordDocInputStream); //Converting the word bytes to com.aspose.word.Document
for (Section section : wordDoc.getSections())
{
    HeaderFooterCollection headerfooters = section.getHeadersFooters();
    if (headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY) == null)
    {
        headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_PRIMARY));
    }
    PageSetup pageSetup = section.getPageSetup();
    if (pageSetup != null)
    {
        if (pageSetup.getDifferentFirstPageHeaderFooter()
        && headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST) == null)
        {
            headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_FIRST));
        }
        if (pageSetup.getOddAndEvenPagesHeaderFooter()
        && headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN) == null)
        {
            headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_EVEN));
        }
    }
    for (HeaderFooter headerFooter : headerfooters)
    {
        int type = headerFooter.getHeaderFooterType();
        if (type == HeaderFooterType.FOOTER_PRIMARY
        || type == HeaderFooterType.FOOTER_FIRST
        || type == HeaderFooterType.FOOTER_EVEN)
        {
            Shape image = new Shape(wordDoc, ShapeType.IMAGE);
            image.setWrapType(WrapType.SQUARE);
            image.setHorizontalAlignment(HorizontalAlignment.CENTER);
            image.getImageData().setImage(new BufferedInputStream(new FileInputStream(pathToImage)));
            // Append a new center-aligned paragraph to each footer present ... 
            Paragraph paragraph = new Paragraph(wordDoc);
            paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
            paragraph.appendChild(image);
            headerFooter.appendChild(paragraph);
        }
    }
}

Hi there,

Thanks for your inquiry. You are adding the image into document’s footer correctly. We have tested the scenario using latest version of Aspose.Words for Java 17.1.0 and have not found the shared issue. Please use Aspose.Words for Java 17.1.0.

Hi,

I downloaded the latest version of this product (17.1.0) but got the same issue.

I have used the same code that I posted here.

See the output document here. As you can see in the document, the image is present on footer of all the pages but user-defined footer is gone from page 2 onwards.

Hi there,

Thanks for your inquiry. Only first section of your input document contains the header/footer. In your case, you need to copy the contents of first header/footer into other section’s header/footer. Following highlighted code snippet shows how to add the contents of header/footer in first section into other section’s header/footer. Hope this helps you.

If you still face problem, please share your expected output Word document here for our reference. We will then provide you more information about your query along with code.

Document wordDoc = new Document(MyDir + "MyDocument_MultipleSectionBreak.docx");
Section FirstSection = wordDoc.getFirstSection();
for (Section section : wordDoc.getSections()) {
    HeaderFooterCollection headerfooters = section.getHeadersFooters();

    if (headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY) == null) {
        headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_PRIMARY));
    }

    PageSetup pageSetup = section.getPageSetup();
    if (pageSetup != null) {
        if (pageSetup.getDifferentFirstPageHeaderFooter()
        && headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST) == null) {
            headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_FIRST));
        }
        if (pageSetup.getOddAndEvenPagesHeaderFooter()
        && headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN) == null) {
            headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_EVEN));
        }
    }

    for (HeaderFooter headerFooter : headerfooters) {
        int type = headerFooter.getHeaderFooterType();
        if (type == HeaderFooterType.FOOTER_PRIMARY
        || type == HeaderFooterType.FOOTER_FIRST
        || type == HeaderFooterType.FOOTER_EVEN) {

            if (FirstSection.equals(section))
            {
                Shape image = new Shape(wordDoc, ShapeType.IMAGE);
                image.setWrapType(WrapType.SQUARE);

                image.setHorizontalAlignment(HorizontalAlignment.CENTER);

                image.getImageData().setImage(new BufferedInputStream(new FileInputStream(MyDir + "in.png")));

                // Append a new center-aligned paragraph to each footer present …
                Paragraph paragraph = new Paragraph(wordDoc);
                paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

                paragraph.appendChild(image);

                headerFooter.appendChild(paragraph);
            }
            else
            {
                for (Node node : (Iterable)FirstSection.getHeadersFooters().getByHeaderFooterType(type).getChildNodes())
                {
                    section.getHeadersFooters().getByHeaderFooterType(type).appendChild(node.deepClone(true));
                }
            }
        }
    }
}

wordDoc.save(MyDir + "Out v17.1.0.docx");

Hi Manzoor,

The solution provided will not work because the assumption we are making here is that header/footers exists only for the first section which is not be the case always.

We may have documents that have different header/footer for different sections.

See this document. It does not have the same header/footer on each page. Page 2 onwards have different header/footer. So if we copy the header/footer from first section to the subsequent sections, it will override other footers.

I did some debugging and I think I know what is going wrong here.

Assumption/Theory :

A page of a document can have multiple sections and each section can have its own header/footer, but since only one header/footer can be visible on the page of the document, so it will select the header/footer of the first section of the page (which has a non-null header/footer value).

Lets take this example :

Document 
Page 1 
Section 1 --> footer 1
Section 2 --> footer 2
Section 3 --> footer 3
Section 4 --> footer 4
Section 5 --> footer 5
Page 2
Section 1 --> null footer
Section 2 --> null footer
Section 3 --> null footer
Section 4 --> footer A
Section 5 --> footer B
Section 6 --> footer C

For the above document, output will show the document as

Document
Page 1 --> Footer is "footer 1"
Page 2 --> Footer is "footer A" //It shows the footer for first section which has non-null footer value.

What we are doing in code I guess is we are removing all the null footers with a “new HeaderFooter” instance and thus the above document is becoming like the this

Document 
Page 1 
Section 1 --> footer 1
Section 2 --> footer 2
Section 3 --> footer 3
Section 4 --> footer 4
Section 5 --> footer 5
Page 2
Section 1 --> new HeaderFooter()
Section 2 --> new HeaderFooter()
Section 3 --> new HeaderFooter()
Section 4 --> footer A
Section 5 --> footer B
Section 6 --> footer C

and now the document will be shown as

Document 
Page1 --> "footer1"
Page2 --> new HeaderFooter() // empty header footer object instead of Footer A… and thus it seems like "Footer A" has been removed by Aspose.

So basically since we replaced the first null footer with an actual instance of HeaderFooter, some footers looked like they are removed from the document.

  1. Do you think my analysis is correct?
  2. If yes, do you know how I can solve the issue?
  3. If you think my analysis is wrong, I have already provided the document in the post, can you help me get a working code to solve the issue.
  4. Do you know if there is way we can get info like
  5. list of sections preent in a given page.
  6. firstSection of every page
  7. header/footer of every page rather than sections

Thanks

Sushant

Hi Manzoor,

Let me know if my analysis is right and what solution do you suggest to solve it.

Hi Sushant,

Thanks for your inquiry. Your shared analysis is correct. We suggest you please read following articles:

Working with Headers and Footers

Joining Documents with Headers and Footers

There is no need to create header/footer of all types in the document. Please check the members of HeaderFooterType class. Please only create the header/footer of type HEADER_PRIMARY and FOOTER_PRIMARY.

If your document contains the header/footer in first section only, we suggest you please use HeaderFooterCollection.LinkToPrevious method to link or unlink all headers and footers to the corresponding headers and footers in the previous section. Please check the following code example.

Document wordDoc = new Document(MyDir + "MyDocument_MultipleSectionBreak.docx");
Section FirstSection = wordDoc.getFirstSection();

for (Section section : wordDoc.getSections())
{
    HeaderFooterCollection headerfooters = section.getHeadersFooters();

    if (headerfooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY) == null)
    {
        headerfooters.add(new HeaderFooter(wordDoc, HeaderFooterType.FOOTER_PRIMARY));
    }

    if (FirstSection.equals(section))
    {
        Shape image = new Shape(wordDoc, ShapeType.IMAGE);
        image.setWrapType(WrapType.SQUARE);
        image.setHorizontalAlignment(HorizontalAlignment.CENTER);
        image.getImageData().setImage(new BufferedInputStream(new FileInputStream(MyDir + "in.png")));

        Paragraph paragraph = new Paragraph(wordDoc);
        paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        paragraph.appendChild(image);

        HeaderFooter headerFooter = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
        headerFooter.appendChild(paragraph);
    }
    else
    {
        headerfooters.linkToPrevious(true);
    }
}

wordDoc.save(MyDir + "Out v17.1.0.docx");

If your document contains the header/footer for all sections, you need to modify the header/footer according to your requirements. You may use HeaderFooterCollection.LinkToPrevious method.

If you simply want to insert the image in the footer of document, you can use the above code example in both cases.