Hello cadev, 

----------------------------------------
> Date: Thu, 27 May 2010 19:29:40 -0700
> From: [email protected]
> To: [email protected]
> Subject: Re: [iText-questions] Add a page number to a PdfPTable
>
> cadev wrote:
>>
>> I am using page events to add a header to a pdf document. The header
>> contains a PdfPTable. I would like to add a String containing a page
>> number to the last row of the PdfPTable. However, when I initialize the
>> PdfPTable (in setPageHeader), the page number does not exist yet -- it
>> only exists in the "onEndPage" method. How can I put the page number in
>> the PdfPTable?
>>
>
> For anyone who is interested, here was my eventual solution. I invite
> anyone to critique my method and come up with a similar solution that is not
> so round-about.
>
> public class ComplexHeader extends PdfPageEventHelper {
>
> private PdfPTable pdfHeaderTable;
> private float pdfHeaderPadding = 0;
> private float headerDataHeight = 0;
> private float hNumberedTableHeight = 0;
> private Phrase headerNumberedPhrase = null;
>
> public ComplexHeader() {
> ; //Do nothing, just initialize the object
> }
>
> public void setPageHeader (PdfPTable iHeaderTable, String numberedString,
> Font font, Document document) {
> pdfHeaderTable = new PdfPTable(iHeaderTable);
> pdfHeaderTable.setTotalWidth(document.right() - document.left());
> headerDataHeight = pdfHeaderTable.getTotalHeight();
> if (font == null)
> font = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
> headerNumberedPhrase = new Phrase(numberedString, font);
> if (!headerNumberedPhrase.isEmpty()) {
> PdfPTable pdfNumberedTable = new PdfPTable(1);
> pdfNumberedTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
> pdfNumberedTable.getDefaultCell().setUseAscender(true);
> pdfNumberedTable.getDefaultCell().setUseBorderPadding(true);
> Phrase headerPhraseWithNumber = new Phrase(headerNumberedPhrase);
> headerPhraseWithNumber.add(" 999"); //This is just to figure out table
> size, and there will probably never be a page number>999
> PdfPCell headerCell = new PdfPCell(pdfNumberedTable.getDefaultCell());
> headerCell.setPhrase(headerPhraseWithNumber);
> headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
> pdfNumberedTable.addCell(headerCell);
> pdfNumberedTable.setTotalWidth(document.right() - document.left());
> hNumberedTableHeight = pdfNumberedTable.getTotalHeight();
> }
> document.setMargins(document.leftMargin(), document.rightMargin(),
> (document.topMargin() + headerDataHeight + hNumberedTableHeight +
> pdfHeaderPadding), document.bottomMargin());
> }
>
> public void onEndPage(PdfWriter writer, Document document) {
> PdfContentByte cb = writer.getDirectContent();
> if (pdfHeaderTable != null) {
> pdfHeaderTable.writeSelectedRows(0, -1, document.left(), document.top() +
> hNumberedTableHeight + headerDataHeight + pdfHeaderPadding, cb);
> if (!headerNumberedPhrase.isEmpty()) {
> PdfPTable pdfNumberedTable = new PdfPTable(1);
> pdfNumberedTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
> pdfNumberedTable.getDefaultCell().setUseAscender(true);
> pdfNumberedTable.getDefaultCell().setUseBorderPadding(true);
> Phrase headerPhraseWithNumber = new Phrase(headerNumberedPhrase);
> headerPhraseWithNumber.add(" " + writer.getPageNumber());
> PdfPCell headerCell = new PdfPCell(pdfNumberedTable.getDefaultCell());
> headerCell.setPhrase(headerPhraseWithNumber);
> headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
> pdfNumberedTable.addCell(headerCell);
> pdfNumberedTable.setTotalWidth(document.right() - document.left());
> pdfNumberedTable.writeSelectedRows(0, -1, document.left(),
> document.top() + hNumberedTableHeight + pdfHeaderPadding, cb);
> }
> }
> }
>
> }

You mentioned (separate post) that you have the first edition of the 
book? Section 14.2.1 goes over the PdfPageEvent methods and how to use them, 
which includes 
onStartPage(). In your ComplexHeader class, not sure what setPageHeade() is 
doing, but instead create a 
protected/private variable to store the page number and increment it in 
onStartPage(). The variable will be available in onEndPage() to write the page 
number.

The new book is really good :)

HTH - keith
                                          
_________________________________________________________________
The New Busy is not the old busy. Search, chat and e-mail from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_3
------------------------------------------------------------------------------

_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to