Hello,

> Date: Mon, 7 Jun 2010 09:43:44 -0400
> From: michelleyzh...@gmail.com
> To: iText-questions@lists.sourceforge.net
> Subject: [iText-questions] question about table header and onOpenDocument
>
> In order to add a table as header, I mimic the code from: 
> http://www.1t3xt.info/examples/browse/?page=example&id=395


You should follow Bruno's code a little better :)


> However, the result I got was not the same as the one on the website. The 
> header and the footer only show once on the first page.
>
> Can anyone help?
>
> Below is my code:
>
> main.cs:
> protected void Button1_Click(object sender, EventArgs e)
> {
> string filename = Server.MapPath("Files/FO-CA-Cert-" + "Juarez" + "-G38-" + 
> "4" + ".pdf");
>
> Document document = new Document(PageSize.A4, 50, 50, 50, 100);
>
> PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, 
> FileMode.Create));
> document.Open();
>
>
> AddTableAsHeaderFooter addTableAsHeaderFooter = new AddTableAsHeaderFooter();


>From here...


> addTableAsHeaderFooter.onOpenDocument(writer, document);
> addTableAsHeaderFooter.OnStartPage(writer, document);
>
> addTableAsHeaderFooter.onEndPage(writer, document);
> addTableAsHeaderFooter.onCloseDocument(writer, document);


^^^^^^^^^^^^^
To above here is not needed. (not in the example code you referenced)


> writer.PageEvent = addTableAsHeaderFooter;


...


> AddTableAsHeaderFooter.cs:


Although C# is very similar to Java, there **are** some differences. The one 
that got you here is the difference in how Java and C# use/implement virtual 
methods.

When you're having problems like this I would suggest you take a look at the 
iTextSharp source code, in some cases it's the best source of documentation. 


> public class AddTableAsHeaderFooter : PdfPageEventHelper
>
> {
>
>
> public PdfTemplate tpl;
> public BaseFont helv;
> public PdfPTable headerTable;


Then you would have figured out that you need to override all 
'OnXXX' (note capitalization) methods below here.




> public void onOpenDocument(PdfWriter writer, Document document)
> {
>
>
> // initializations
>
> tpl = writer.DirectContent.CreateTemplate(150, 18);
> Rectangle rect = new Rectangle(0, 0, 150, 18);
> rect.BackgroundColor = iTextSharp.text.BaseColor.GRAY;
> tpl.BoundingBox = rect;
>
> tpl.Rectangle(rect);
> helv = BaseFont.CreateFont("Helvetica", BaseFont.WINANSI, false);
> // header
> headerTable = new PdfPTable(1);
> PdfPCell cell = new PdfPCell(new Paragraph("Header Text"));
>
> headerTable.AddCell(cell);
>
> headerTable.TotalWidth = document.Right - document.Left;
> headerTable.LockedWidth = true;
> }
>
> public void onEndPage(PdfWriter writer, Document document)
> {
>
> // Header
> headerTable.WriteSelectedRows(0, -1, document.LeftMargin,
> document.Top + headerTable.TotalHeight,
> writer.DirectContent);
>
> // Footer
> PdfPTable footerTable = new PdfPTable(2);
> PdfPCell cell1 = new PdfPCell(new Phrase("page " + writer.PageNumber));
> footerTable.AddCell(cell1);
>
> PdfPCell cell2 = new PdfPCell(new Phrase("page " + writer.PageNumber + " of 
> "));
> footerTable.AddCell(cell2);
> footerTable.TotalWidth = document.Right - document.Left;
>
> footerTable.WriteSelectedRows(0, -1, document.LeftMargin,
> document.BottomMargin, writer.DirectContent);
> }
>
> public void onCloseDocument(PdfWriter writer, Document document)
> {
> tpl.BeginText();
> tpl.SetFontAndSize(helv, 12);
> tpl.SetTextMatrix(2, 4);
> tpl.ShowText("Number of pages = " + (writer.PageNumber - 1));
> tpl.EndText();
> }
>
>
> }

HTH - keith
                                          
_________________________________________________________________
The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4
------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
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