Page is missing after insert image

The code:

String path = "C:\\Users\\z_jia\\Desktop\\a\\DMS\\";
Document word = new Document(path + "1.docx");
var builder = new DocumentBuilder(word);
for (int i = 0; i < word.getSections().getCount(); i++)
{
    builder.moveToSection(i);
    builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
    var shape = builder.insertImage(path + "1.png");
    shape.setWrapType(WrapType.NONE);
    shape.setBehindText(true);
    var ps = word.getSections().get(i).getPageSetup();
    shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    double top = 20;
    double left = 20;
    left = ps.getPageWidth() - shape.getWidth() - 15;
    top = ps.getPageHeight() - shape.getHeight() - 25;

    shape.setLeft(left);
    shape.setTop(top);
}

word.save(path + "1result.docx");

1.docx (103.1 KB)

1.png (109 Bytes)

1result.docx (106.5 KB)

The page in second section is missing.

@GusGus

You are inserting the image into the primary footer of each section, and the second‑section page disappears because both sections are laid out on the same physical page. When this happens Microsoft Word shows only the footer of the first section, and Aspose.Words cannot change that layout behavior [2].

What you can do

  1. Place the second section on a new page – set the second section to start on a new page (e.g., section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE)) or insert a manual page break before it. This forces each section onto its own page, so Word will render both footers and the missing page will reappear.

  2. If the sections must stay on the same page – the footer of the second section cannot be displayed, as this is a limitation of Word’s rendering and cannot be overridden by Aspose.Words.

@GusGus This is an expected behavior. This occurs because the second section actually does not have primary footer, so it is inherited from the previous section. When you do builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY) Aspose.Words creates the footer and it is no longer inherited from the previous section. Please try modifying your code like this:

String path = "C:\\Temp\\";
Document word = new Document(path + "1.docx");
DocumentBuilder builder = new DocumentBuilder(word);
for (int i = 0; i < word.getSections().getCount(); i++)
{
    builder.moveToSection(i);
    if (builder.getCurrentSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY) != null)
    {
        builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
        Shape shape = builder.insertImage(path + "1.png");
        shape.setWrapType(WrapType.NONE);
        shape.setBehindText(true);
        PageSetup ps = word.getSections().get(i).getPageSetup();
        shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
        double top = 20;
        double left = 20;
        left = ps.getPageWidth() - shape.getWidth() - 15;
        top = ps.getPageHeight() - shape.getHeight() - 25;

        shape.setLeft(left);
        shape.setTop(top);
    }
}

word.save(path + "1result.docx");

it works, thanks

1 Like