Skip to content

The header and footer are removed when PdfDocument#newPage() is called explicitly immediately after it was called implicitly. #1509

@BenJ1337

Description

@BenJ1337

Describe the bug

If newPage() is called explicitly after it has already been called implicitly because the previous page was full, the header and footer are removed from the new page.

To Reproduce

        Path output = Path.of("openpdf-test.pdf");
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(
                document,
                new FileOutputStream(output.toFile())
        );
        document.setHeader(new com.lowagie.text.HeaderFooter(false, new Phrase("Header")));
        document.setFooter(new com.lowagie.text.HeaderFooter(false, new Phrase("Footer")));
        document.open();
        Font font = new Font(Font.HELVETICA, 12);

        for (int i = 0; i < 50; i++) {
            if(i == 37) {
                // newPage() has already been called when the following line is executed, because Paragraph 37 is the first line on the new page.
                document.newPage();
            }
            document.add(new Paragraph(
                    "This is line " + i + " of a long text to force automatic page breaks.",
                    font
            ));
        }
        document.close();

Expected behavior

Header and footer should not be removed.

add initPage(); to PdfDocument after line 874:

    public boolean newPage() {
        lastElementType = -1;
        if (isPageEmpty()) {
            setNewPageSizeAndMargins();
            resetText(true);
            initPage();
            return false;
        }
     ...

System

  • OS: Windows / Linux
  • OpenPDF version: master

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions