2

I'm using iText 5.3.5 to create a pdf document. Right now I am trying to get a rectangle on every single page of the document but I'm not pretty sure of how to do this. I tried adding this at the end of my code (I found it on the internet):

PdfContentByte cb = writer.getDirectContent();
for (int pgCnt = 1; pgCnt <= writer.getPageNumber(); pgCnt++) {
    cb.saveState();
    cb.setColorStroke(new CMYKColor(1f, 0f, 0f, 0f));
    cb.setColorFill(new CMYKColor(1f, 0f, 0f, 0f));
    cb.rectangle(20,10,10,820);
    cb.fill();
    cb.restoreState();
}     

but this only adds the rectangle on the last page and it kind of make sense because I'm not using the pgCnt anywhere. How can I specify that I want the rectangle on page number pgCnt, so I can add the rectangle on every page?

Hope I explained myself. Thanks in advance for your help. :)

1
  • I assume the code you found on the internet was used with some PdfStamper editing an already existing PDF while you added it to code using a PdfWriter creating a new PDF. In that case the sample code should be used not merely once but instead (by means of a page event listener) once per page.
    – mkl
    May 19, 2013 at 21:34

1 Answer 1

2

Please take a look at the entries for the keyword Page events on the official iText site. You need to extend the PdfPageEventHelper class and add your code to the onEndPage() method.

public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    cb.setColorStroke(new CMYKColor(1f, 0f, 0f, 0f));
    cb.setColorFill(new CMYKColor(1f, 0f, 0f, 0f));
    cb.rectangle(20,10,10,820);
    cb.fill();
    cb.restoreState();
}

Create an instance of your custom page event class, and declare it to the writer before opening the document:

writer.setPageEvent(myPageEventInstance);

Now your rectangle will be drawn on every page, on top of the existing content. If you want the rectangle under the existing content: replace getDirectContent() with getDirectContentUnder().

Take a look at the Stationery example if you need some working source code. Please consult the official iText site in the future instead of saying you've found something "on the internet" without mentioning the source.

3
  • What is the use of cb.restoreState(); ? Jan 28, 2016 at 8:20
  • @DurgeshPandey we used saveState() before we change the state (we define new stroke and fill colors). Once we're done drawing content using those colors, we return to the original state (before the colors were changed) by using restoreState(). You can't use restoreState() without having a saveState() first. You shouldn't use saveState() without it being followed by a restoreState() later on. Jan 28, 2016 at 8:32
  • 1
    I think cb.fill() should be replaced by cb.fillStroke() (if you set the stroke colour, I guess you intend to draw the border...)
    – leonbloy
    Aug 25, 2018 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.