In order to add a table as header, I mimic the code from:
http://www.1t3xt.info/examples/browse/?page=example&id=395
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();
addTableAsHeaderFooter.onOpenDocument(writer, document);
addTableAsHeaderFooter.OnStartPage(writer, document);
addTableAsHeaderFooter.onEndPage(writer, document);
addTableAsHeaderFooter.onCloseDocument(writer, document);
writer.PageEvent = addTableAsHeaderFooter;
String text = "Lots of text. ";
for (int i = 0; i < 5; i++)
text += text;
for (int i = 0; i < 20; i++)
document.Add(new Paragraph(text));
document.Close() ;
Response.ContentType = "text/pdf";
Response.AppendHeader("Content-Disposition", "attachment;
filename=test.pdf");
Response.TransmitFile("Files/FO-CA-Cert-Juarez-G38-4.pdf");
Response.End();
}
AddTableAsHeaderFooter.cs:
public class AddTableAsHeaderFooter : PdfPageEventHelper
{
public PdfTemplate tpl;
public BaseFont helv;
public PdfPTable headerTable;
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();
}
}
--
---------------------------
Michelle Zhang
------------------------------------------------------------------------------
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/