Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

Re: [iText-questions] Questions On Headers/Footers with PageEvents

John Zeitler

2007-10-02

Replies:


As I'd said, it's a direct copy/paste from the linked tutorial. I'll
reproduce it here anyway:

  /**
  * @see
com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
com.lowagie.text.Document)
  */
  public void onEndPage(PdfWriter writer, Document document) {
    try {
       Rectangle page = document.getPageSize();
       PdfPTable head = new PdfPTable(3);
       for (int k = 1; k <= 6; ++k)
          head.addCell("head " + k);
       head.setTotalWidth(page.getWidth() - document.leftMargin() -
document.rightMargin());
       head.writeSelectedRows(0, -1, document.leftMargin(),
page.getHeight() - document.topMargin() + head.getTotalHeight(),
          writer.getDirectContent());
       PdfPTable foot = new PdfPTable(3);
       for (int k = 1; k <= 6; ++k)
          foot.addCell("foot " + k);
       foot.setTotalWidth(page.getWidth() - document.leftMargin() -
document.rightMargin());
       foot.writeSelectedRows(0, -1, document.leftMargin(),
document.bottomMargin(),
          writer.getDirectContent());
    }
    catch (Exception e) {
       throw new ExceptionConverter(e);
    }
  }


Paulo Soares wrote:
>
> Where's the onEndPage method code?
>
> Paulo
>
> ----- Original Message -----
> From: "John Zeitler" <john.s.zeitler@(protected)>
> To: <itext-questions@(protected)>
> Sent: Tuesday, October 02, 2007 7:33 PM
> Subject: [iText-questions] Questions On Headers/Footers with PageEvents
>
>
>>
>> Hi,
>>
>> I've run into a bit of trouble with headers and footers. I started off
>> using setHeader(HeaderFooter), then saw that that was apparently 'old
>> functionality' and that I should be using page events. So, I created a
>> bare-bones event class based explicitly on the example from EndPage.java
>> in
>> the iText tutorial (here:
>> http://itextdocs.lowagie.com/tutorial/directcontent/pageevents/index.html
>> ).
>> I literally just copied the onEndPage method from that one and put it
>> into
>> my new class; it's the only method in the class. My problem is that the
>> event is being called and handled by the class, but no output is being
>> written by that method (the rest of the output is OK). Here's what I'm
>> doing (abstracting out unimportant stuff):
>> Java code-ish:
>> /* assuming OutputStream out is passed in to this, the document
>> generator;
>> * out is the OutputStream from the HttpServletResponse */
>>
>> Document document = new Document([page size], [margins all set to 0]);
>> PdfWriter writer = PdfWriter.getInstance(document, out);
>> writer.setPageEvent(new myPageEventHandler()); //this has the onEndPage
>> from
>> EndPage.java
>> document.open();
>>
>> /* from here is contained in a bean which renders its data as a table;
>> document is passed in */
>>
>> document.newPage();
>>
>> Phrase titleText = new Phrase("This is a bold title that is NOT to be a
>> header");
>> document.add(titleText);
>>
>> PdfPTable table = new PdfPTable(3);
>> table.setTotalWidth([page size.width * a margin ratio]);
>> table.setLockedWidth(true);
>>
>> PdfPCell cell = null;
>> /* assume that this could be hundreds or thousands of rows long */
>> for(int i = 0; i<9999; i++) {
>> cell = new PdfPCell(new Phrase("celldata " + i));
>> /* cell modifications, like border width, rowspan, etc. */
>> table.addCell(cell);
>> }
>>
>> document.add(table);
>>
>> /* now this is performed by the class which calls the bean's method
>> (above)
>> */
>>
>> if(document != null) document.close();
>> if(out != null) out.close();
>>
>> //end Java code
>>
>> I suspect there's something going on with the mixing of the
>> writeSelectedRows in the onEndPage method; if I replace that line with
>> document.add(head) etc. I can write to the page, but the problem is that
>> those don't show up on the first page, just the second page and
>> thereafter.
>> More to the point, just using document.add doesn't allow me to set the
>> positioning of the headers and footers. As a final bit of information,
>> I'm
>> using iText 2.0.5 after upgrading from 1.1.4; I've quashed all other
>> bugs,
>> but this one is the last one (headers and footers didn't work for me in
>> the
>> old version either). This is driving me crazy and I'm looking for any
>> help
>> or advice that can be offered. Thanks!
>>
>> John Z.
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2005.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> iText-questions mailing list
> iText-questions@(protected)
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> Buy the iText book: http://itext.ugent.be/itext-in-action/
>
>

--
Sent from the iText - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
©2008 junlu.com - Jax Systems, LLC, U.S.A.