You can't add page x or y until you close the document...think about
it...you don't know how many pages are in the document till it's closed
If you really just want a page x of y I would recommend something like
this:
//produce PDF
doc.close();
PdfReader reader = new PdfReader(//read your original PDF);
PdfStamper stamper = new PdfStamper(reader, [use a stream of some
type]);
PdfContentByte under = null;
int totalPages = reader.getNumberOfPages();
for (int page = 1; page <= totalPages; page++) {
under = stamper.getUnderContent(page);
String pageXofY = String.format("Page %d of %d", page,
totalPages);
ColumnText.showTextAligned( // add pageXofY to the document
being written by the stamper )
}
reader.close();
stamper.close();
//save stamper bytes if writing to memory
Jason
-----Original Message-----
From: Aarthi [mailto:[email protected]]
Sent: Tuesday, December 20, 2011 4:47 PM
To: [email protected]
Subject: [iText-questions] page numbers in footer
Hi,
I have an issue with displaying the page number in the PDF. My
requirement is to display page number like "Page 1 of 20" and so on...
I have used the inner class explained in chapter05, but removed the code
that i dont want.
My inner class looks like this,
class HeaderFooter extends PdfPageEventHelper {
/** Current page number (will be reset for every chapter). */
int pageNum;
PdfTemplate total;
/**
* Increase the page number.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(
* com.itextpdf.text.pdf.PdfWriter,
com.itextpdf.text.Document)
*/
public void onStartPage(PdfWriter writer, Document document) {
pageNum++;
}
/**
* Adds the header and the footer.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter,
com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("art");
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, new Phrase(String.format("Page
%d of", writer.getPageNumber())),
(rect.getLeft() + rect.getRight()) / 2,
rect.getBottom()
- 18, 0);
}
}
my issue is i get this Page # of independent pages correct but not the
total page number.
The one that gets displayed on my pages is, "page 1 of ","page 2 of ".
I dont see the count of total pages. I beleive "write.getPageNumber()"
gets the total pages in the pdf.
Not sure where im wrong. can someone please help me with this?
Thanks in advance.
--
View this message in context:
http://itext-general.2136553.n4.nabble.com/page-numbers-in-footer-tp4219
499p4219499.html
Sent from the iText - General mailing list archive at Nabble.com.
------------------------------------------------------------------------
------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a
reference to the iText book: http://www.itextpdf.com/book/ Please check
the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php
This is a transmission from Purdue Federal Credit Union (Purdue Federal) and is
intended solely for its authorized recipient(s), and may contain information
that is confidential and or legally privileged. If you are not an addressee, or
the employee or agent responsible for delivering it to an addressee, you are
hereby notified that any use, dissemination, distribution, publication or
copying of the information contained in this email is strictly prohibited. If
you have received this transmission in error, please notify us by telephoning
(765)497-3328 or returning the email. You are then instructed to delete the
information from your computer. Thank you for your cooperation.
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php