How to add image background to pdf for every page?

前端 未结 2 1553
轮回少年
轮回少年 2020-12-07 00:07

I\'m trying to set a background to a pdf and managed to set it with an image my pdf has a big table so the pages are added automatically not with the Document.NewPage(

相关标签:
2条回答
  • 2020-12-07 00:48

    go for

    PdfPageEvent.onStartPage()

    . In this event, write your code to insert the image (as you are doing it). What it will do is that as soon as a new page is created, it will add the image to it and then the content over it; giving a watermark effect.

    0 讨论(0)
  • 2020-12-07 00:53

    Whenever you want to apply something to every page, you should use page events, more specifically PdfPageEvent.onEndPage(), to do it. You can find samples for its usage by keyword Page events > onEndPage --- these samples are taken from iText in Action 2nd Edition. The samples mainly add footers and headers while you want to add background graphics.

    Be aware that you shouldn't add content to the Document instance here but instead directly to the PdfWriter, and as you want the image to be under the page content , not above it, you will need to use PdfWriter.getDirectContentUnder() like in the sample Stationery and not PdfWriter.getDirectContent() like in the other samples.

    PS: The analogous samples for .Net can be found here.

    PPS: The sample ImageDirect.java / ImageDirect.cs shows how to add an image to some direct content which might be the information missing here.

    0 讨论(0)
提交回复
热议问题