PDF footer below using iTextSharp

I am trying to create a pdf document in C # using iTextSharp 5.0.6. I want to add a header and footer to each page in the OnStartPage and OnEndPage events, respectively.

In the case of the footer, there is the problem that the footer is created exactly where the page ends, whereas I would like to be at the bottom of the page.

Is there a way in iTextSharp to specify the height of the page, so that the footer is always created at the bottom.

Thanks!

+4
source share
1 answer

Page height is always determined by:

document.PageSize.Height // document.getPageSize().getHeight() in Java 

Keep in mind that in PDF 0.0 there is a lower left corner, and the coordinates increase as you move right and up.

Inside PdfPageEvent you need to use absolute coordinates. It looks like you are either getting the current Y from the document, or just drawing the material at the current location. Do not do that.

Also, if you want to use the same exact footer on each page, you can draw everything in a PdfTemplate, and then draw this template on the different pages on which you want.

 PdfTemplate footerTmpl = writer.getDirectContent().createTemplate( 0, 0, pageWidth, footerHeight ); footerTmpl.setFontAndSize( someFont, someSize ); footerTmpl.setTextMatrix( x, y ); footer.showText("blah"); // etc 

Then in your PdfPageEvent you can simply add footerTempl at the bottom of the page:

  writer.getDirectContent().addTemplateSimple( footerTmpl, 0, 0 ); 

Even if most of your footer is the same, you can use this technique to save memory, runtime, and file size.

In addition, if you do not want to interact directly with the PdfContentByte drawing PdfContentByte , you can avoid them to some extent through ColumnText . There are several SO questions tagged by iText or iTextSharp pertaining to this class. Knock, you will find them.

+4
source

Source: https://habr.com/ru/post/1343485/


All Articles