java - How can I add Rectangle on every page of a document using iText? -


i'm using itext 5.3.5 create pdf document. right trying rectangle on every single page of document i'm not pretty sure of how this. tried adding @ end of code (i found on internet):

pdfcontentbyte cb = writer.getdirectcontent(); (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 adds rectangle on last page , kind of make sense because i'm not using pgcnt anywhere. how can specify want rectangle on page number pgcnt, can add rectangle on every page?

hope explained myself. in advance help. :)

please take @ entries keyword page events on official itext site. need extend pdfpageeventhelper class , add code 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 instance of custom page event class, , declare writer before opening document:

writer.setpageevent(mypageeventinstance); 

now rectangle drawn on every page, on top of existing content. if want rectangle under existing content: replace getdirectcontent() getdirectcontentunder().

take @ stationery example if need working source code. please consult official itext site in future instead of saying you've found "on internet" without mentioning source.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -