OpenPDF
OpenPDF copied to clipboard
Header and Footer phrase is no longer being rendered
Describe the bug The document header and footer are no longer being rendered in version 1.3.29 when solely set with Phrase(). However, if you change the "numbered" option to TRUE then the header and footer are rendered but only with the page number. The supplied Phrase() is not being rendered.
To Reproduce Document document = new Document( PageSize.Letter ); PdfWriter.getInstance( document, new FileOutputStream( "/tmp/output.pdf" ) );
/* Set document properties */ document.addAuthor( "Author" ); document.addCreator( this.getClass().getSimpleName() ); document.addCreationDate(); document.addKeywords( "some,key,words" ); document.addSubject( "Subject" ); document.addTitle( "Title" ); document.setDocumentLanguage( "ENG" );
/* Header */ Font headerFont = FontFactory.getFont( FontFactory.COURIER ); headerFont.setSize( 16.0f ); headerFont.setStyle( Font.BOLD ); HeaderFooter header = new HeaderFooter(new Phrase( "This is a header.", headerFont ), false ); header.setAlignment( Element.ALIGN_CENTER ); header.setBorder( Rectangle.NO_BORDER ); document.setHeader( header );
/* Footer */ Font footerFont = FontFactory.getFont( FontFactory.COURIER ); footerFont.setSize( 8.0f ); HeaderFooter footer = new HeaderFooter( new Phrase( "This is a footer.", footerFont ), false ); // does not render anything // HeaderFooter footer = new HeaderFooter( new Phrase( "This is a footer.", footerFont ), true ); // only page# rendered footer.setAlignment( Element.ALIGN_CENTER ); footer.setBorder( Rectangle.NO_BORDER ); document.setFooter( footer );
document.open();
/* Body */ Font bodyFont = FontFactory.getFont( FontFactory.COURIER ); bodyFont.setSize( 12.0f );
document.add( new Paragraph( "Hello World", bodyFont ) ); document.add( Chunk.NEXTPAGE ); document.add( new Paragraph("Hello new page.", bodyFont ) );
/* Close */ document.close();
Expected behavior Header and footer phrase should also render.
Screenshots
System (please complete the following information):
- OS: Linux Ubuntu 20.04.4 LTS
- Used Font: COURIER (and also tried HELVETICA)
- OpenPDF Version: 1.3.29 (via Maven)
- Java Version: openjdk 17.0.3 2022-04-19 (amd64)
- IDE: IntelliJ IDEA 2022.1.3 Ultimate
Additional context
I forgot to mention that if I revert back to OpenPDF Version: 1.3.28 (via Maven) then the header/footer work properly.
can confirm same behavior on macOS (12.1) with openjdk 17.0.1; also works here w/ version 1.3.28
this is most likely due to this commit https://github.com/LibrePDF/OpenPDF/commit/ea9e5c600f8ede086135a933a250b1ecd534d09e it removes the only content from header and footer I have the same problem as the original reporter.
Workaround: wrap the Phrase of the Header/Footer in a Paragraph
The reason seems to be that this was tested with a Paragraph as header content, when adding a paragraph to the header a newline Chunk is created that needs to be removed afterwards.
This code will not work with a naked Phrase though, although the API does suggest otherwise.
Pull requests are welcome.