aspose file tools*
The moose likes Java in General and the fly likes How do I get page numbers to my PDF document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I get page numbers to my PDF document" Watch "How do I get page numbers to my PDF document" New topic
Author

How do I get page numbers to my PDF document

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

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.

Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
"onOpenDocument" is not the correct event - how often do you think it fires?

Try "onEndPage" instead.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Dec 09, 2011
Posts: 155

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Dec 09, 2011
Posts: 155

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

Stanley, when you UseCodeTags, you should first select the text you want to put in code tags before pressing the "Code" button.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

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.
 
subject: How do I get page numbers to my PDF document