• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I get page numbers to my PDF document

 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing a conversion of text file to Pdf and I want them to have PAge Numbers in the format of page 2 for 24 etc. Apparently I have Page NUmber dsplaying only on the first Page. Here is My class For HeaderAndFooter. Please Help.
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"onOpenDocument" is not the correct event - how often do you think it fires?

Try "onEndPage" instead.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:"onOpenDocument" is not the correct event - how often do you think it fires?

Try "onEndPage" instead.

Thank you Tim for Your Response. The OnEndPage Displays other pages. Am wondering wheather there is a problem with my switch(writer.getPageNumber() % 2) { because the document is only displaying the odd number of pages. I want it to display al of them and if possible, Show for example page 4 of 10 where 10 is the total number of pages. I will really appreaciate. Thank you in advance.
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show for example page 4 of 10 where 10 is the total number of pages.

You probably have to do this after the document has been created (in an onCloseDocument handler) because only then is the number of pages known (unless you somehow know beforehand how many pages you're going to end up with). You could create the document in memory without the footer, and then use a PdfReader and a PdfStamper to add the footer on all pages. There is another approach, but it's too involved if you're just starting out with iText; chapter 14.2 of iText in Action (first ed.) goes into more detail about all of this. IMO, you really need that book if you're serious about working with iText; it'll save you much time trying to hunt down information on the 'net.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim For Your Great Ideas. I am Surprised This onCloseDocument Event Fires only Once at the End of the Document while I wanted every p-age to have page x of y. What I currently have is a document of four pages. The first page is displying : Page 1 of (blank), the second page is displaying Page 2 of (blank), the third Page is displaying Page 3 of (blank) but the fouth page is displaying Page 4 of 4 showing that This even is firing only once. While I try it on End Page it is displaying something totally absurd. This is the code I have. Please Tel me where My error is.
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley Mungai wrote:I am Surprised This onCloseDocument Event Fires only Once at the End of the Document

Why? How many "close document" events did you expect to fire for a document? A page is not a document.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim I noticed that will fire only once. But isn't there a way to get the Total Number of pages onCloseDocument and then make the event run at the endPage for example, Can't I have


I have Added totalpages parameter(fictitiously). AM wondering whether there is no way of getting totalpages on closeDocument and then having the Parameter at the endPage Event?
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. closeDocument fires after all pages have been added, and thus at a time when all endPage events have been fired.

See my second posts for two ways to do this.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stanley, when you UseCodeTags, you should first select the text you want to put in code tags before pressing the "Code" button.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thank you ROB I got it am a bit new here so I didn't Know.............Tim, I really Appreciate you for Helping me to think along my Problem. I got a way round and amazingly Not in any of the events of onCloseDocument or on endPage Event, As a compliment to anyone who might be having this problem in the future, am going to post part of the code I used to create Page numbers Page x of y Where x is the page number and Y is the Total NUmber of pages. I hope this helps anyone ele coming here in the Future with the same Issue. And Tim Moore you are a good man. Thank you Sir and God bless you.

Here is the code




 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic