Satinder singh
Satinder singh

Reputation: 10198

Add pdfpTable at footer while creating PDF using iTextsharp

In my Asp.net application i have to generate PDF so i did this using Itextsharp 4.0 which is free Open source,I refer this to set hearder/footer on the PDF generated,

but now my probelm is i am not able to add pdfPtable inside footer,any suggestion how to insert table at footer ?

Code On Page_Load

CreatePDF("Invoice", dtaddproduct, "Prince", "1313", "04/09/2013","4000");

On CreatePDF method i am calling

 TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter();
  writer.PageEvent = PageEventHandler;

before document.Open();

The custom HeaderFooter code (TwoColumnHeaderFooter):

    public override void OnStartPage(PdfWriter writer, Document document)
    {
    base.OnStartPage(writer, document);

    Rectangle pageSize = document.PageSize;

    if (Title != string.Empty)
    {
    cb.BeginText();
    cb.SetFontAndSize(bf, 15);
    cb.SetRGBColorFill(50, 50, 200);
    cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
    cb.ShowText(Title);
    cb.EndText();
    }

    if (HeaderLeft + HeaderRight != string.Empty)
    {
    PdfPTable HeaderTable = new PdfPTable(4);
    HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
    HeaderTable.TotalWidth = pageSize.Width - 80;
    HeaderTable.SetWidthPercentage(new float[] { 45, 45, 45, 45 }, pageSize);

    PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
    HeaderLeftCell.Padding = 5;
    HeaderLeftCell.PaddingBottom = 8;
    HeaderLeftCell.BorderWidthRight = 1;
    HeaderTable.AddCell(HeaderLeftCell);

    PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
    HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
    HeaderRightCell.Padding = 5;
    HeaderRightCell.PaddingBottom = 8;
    HeaderRightCell.BorderWidthLeft = 1;
    HeaderTable.AddCell(HeaderRightCell);

    cb.SetRGBColorFill(0, 0, 0);

    PdfPTable table = new PdfPTable(3);
    table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
    PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
    cell.Colspan = 3;
    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
    table.AddCell(cell);
   string billNumber = HttpContext.Current.Session["billlno"].ToString();
    string billDate = HttpContext.Current.Session["billdate"].ToString();
    string TotalRate = HttpContext.Current.Session["totalhours"].ToString();
    string customerName = HttpContext.Current.Session["customername"].ToString();
    table.AddCell("Date: " + billDate);
    table.AddCell("Bill No: " + billNumber);
    Chunk ck=new Chunk("one\ntwo\nthree\nfour");
     ck.SetBackground(Color.RED);
     Phrase p = new Phrase();
    p.Add(ck);
    PdfPCell cl = new PdfPCell();
    cl.AddElement(p);
    cl.BackgroundColor = new Color(0, 150, 0);
    table.AddCell(cl);
   table.AddCell("Total : "+TotalRate);
    table.AddCell("M/s : "+customerName);
    table.AddCell("Col 2 Row 2");
    table.AddCell("Col 3 Row 2");
    table.AddCell("Col 1 Row 3");
    table.AddCell("Col 2 Row 3");
    table.AddCell("Col 3 Row 3");
    table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);
   }
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
    base.OnEndPage(writer, document);

    int pageN = writer.PageNumber;
    String text = "Page " + pageN + " of ";
    float len = bf.GetWidthPoint(text, 8);
    Rectangle pageSize = document.PageSize;
    cb.SetRGBColorFill(100, 100, 100);

    cb.BeginText();
    cb.SetFontAndSize(bf, 8);
    cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
    cb.ShowText(text);
    cb.EndText();

    cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
    cb.BeginText();
    cb.SetFontAndSize(bf, 8);
    cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
    "Printed On " + PrintTime.ToString(),
    pageSize.GetRight(40),
    pageSize.GetBottom(30), 0);
    cb.EndText();
    }

    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
    base.OnCloseDocument(writer, document);
    template.BeginText();
    template.SetFontAndSize(bf, 8);
    template.SetTextMatrix(0, 0);
    template.ShowText("" + (writer.PageNumber - 1));
    template.EndText();
    }

EDITED CODE (TwoColumnHeaderFooter):

I edit my code placed at OnEndPage. here i am getting same problem, have a look at screenshot there you will find a link More Information is displaying and not the table which i have also written in code. help pls

public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);

        Rectangle pageSize = document.PageSize;
        if (HeaderLeft + HeaderRight != string.Empty)
        {
            PdfPTable HeaderTable = new PdfPTable(2);
            HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
            HeaderTable.TotalWidth = pageSize.Width - 80;
            HeaderTable.SetWidthPercentage(new float[] { 45, 45 }, pageSize);
            PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
            HeaderLeftCell.Padding = 5;
            HeaderLeftCell.PaddingBottom = 8;
            HeaderLeftCell.BorderWidthRight = 0;
            HeaderTable.AddCell(HeaderLeftCell);
            PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
            HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
            HeaderRightCell.Padding = 5;
            HeaderRightCell.PaddingBottom = 8;
            HeaderRightCell.BorderWidthLeft = 0;
            HeaderTable.AddCell(HeaderRightCell);
            cb.SetRGBColorFill(0, 0, 0);
            PdfPTable table = new PdfPTable(3);
            table.TotalWidth = document.PageSize.Width - document.LeftMargin;
            //    table.DefaultCell.Border = PdfPCell.NO_BORDER;
            PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
            cell.Colspan = 3;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            table.AddCell(cell);
            string billNumber = HttpContext.Current.Session["billlno"].ToString();
            string billDate = HttpContext.Current.Session["billdate"].ToString();
            string TotalRate = HttpContext.Current.Session["totalhours"].ToString();
            string customerName = HttpContext.Current.Session["customername"].ToString();
            string address = "Thane west, Street no=30, Mumbai ";
            Font verdana = FontFactory.GetFont("Verdana", 12, Font.BOLD, new Color(76, 177, 255));
            Font subheadfont = FontFactory.GetFont("Verdana", 9, Font.BOLD, new Color(68, 68, 68));
            Font normaltext = FontFactory.GetFont("Verdana", 9, Font.NORMAL, new Color(68, 68, 68));
            Font whiteFont = FontFactory.GetFont("Verdana", 10, Font.NORMAL, new Color(255, 255, 255));
            Chunk ckInvoice = new Chunk("INVOICE", verdana);
            Chunk ck4 = new Chunk("\n");
            Chunk ckcuslbl = new Chunk("CLIENT :", subheadfont);
            Chunk ckaddlbl = new Chunk("ADDRESS :", subheadfont);
            Chunk ckinvlbl = new Chunk("INVOICE #:", subheadfont);
            Chunk ckdatelbl = new Chunk("DATE :", subheadfont);
            Chunk ckcusname = new Chunk(customerName, normaltext);
            Chunk ckaddr = new Chunk(address, normaltext);
            Chunk ckinvoice = new Chunk(billNumber, normaltext);
            Chunk ckdate = new Chunk(billDate, normaltext);
            float[] widthColumnas = new float[] { 25f, 150f };
            PdfPTable nested = new PdfPTable(2);
            nested.DefaultCell.Border = PdfPCell.NO_BORDER;
            nested.SetWidths(widthColumnas);
            nested.AddCell(new Phrase(ckInvoice));
            nested.AddCell(new Phrase(ck3));
            nested.AddCell(new Phrase(ck3));
            nested.AddCell(new Phrase(ck3));
            nested.AddCell(new Phrase(ckcuslbl));
            nested.AddCell(new Phrase(ckcusname));
            nested.AddCell(new Phrase(ckinvlbl));
            nested.AddCell(new Phrase(ckinvoice));
            nested.AddCell(new Phrase(ckdatelbl));
            nested.AddCell(new Phrase(ckdate));
            nested.AddCell(new Phrase(ckaddlbl));
            nested.AddCell(new Phrase(ckaddr));
            PdfPCell nesthousing = new PdfPCell(nested);
            nesthousing.Padding = 0f;
            nesthousing.BorderWidth = 0;
            nesthousing.Colspan = 2;
            table.AddCell(nesthousing);
             Chunk ck = new Chunk("Honda acresone\nThane(W). Mumbai \nStreet rno.20\n022 5023233", whiteFont);
            Paragraph p = new Paragraph();
            p.Add(ck);
            p.Alignment = Element.ALIGN_RIGHT;
            PdfPCell cl = new PdfPCell();
            cl.AddElement(p);
            cl.BackgroundColor = new Color(76, 177, 255);
            cl.FixedHeight = 100.0f;
            cl.HorizontalAlignment = 0;
            cl.PaddingRight = 35;
            cl.PaddingTop = 10;
            cl.VerticalAlignment = Element.ALIGN_TOP;
            table.AddCell(cl);
            table.AddCell("Total : " + TotalRate);
            table.AddCell("M/s : " + customerName);
            table.AddCell("Col 2 Row 2");
            table.AddCell("Col 3 Row 2");
            table.AddCell("Col 1 Row 3");
            table.AddCell("Col 2 Row 3");
            table.AddCell("Col 3 Row 3");
            table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);

        }

        int pageN = writer.PageNumber;
        String text = "Page " + pageN + " of ";
        float len = bf.GetWidthPoint(text, 8);

       // Rectangle pageSize = document.PageSize;
        cb.SetRGBColorFill(100, 100, 100);
        //Create our ColumnText bound to the canvas
        var ct = new ColumnText(cb);
        //Set the dimensions of our "box"
        ct.SetSimpleColumn(pageSize.GetRight(200), pageSize.GetBottom(30), pageSize.Right, pageSize.Bottom);
        //Create a new chunk with our text and font
        var c = new Chunk("More Information", new iTextSharp.text.Font(bf, 10));
        //Set the chunk's action to a remote URL
        c.SetAction(new PdfAction("http://www.google.com"));
        //Add the chunk to the ColumnText
        ct.AddElement(c);
        //Tell the ColumnText to draw itself
        ct.Go();

        PdfPTable table2 = new PdfPTable(3);
        table2.TotalWidth = document.PageSize.Width - document.LeftMargin;
        table2.AddCell("cell Value 1");
        table2.AddCell("Cell Value 2 ");
        table2.WriteSelectedRows(0, -1, document.LeftMargin + 200, document.PageSize.Height - 30, writer.DirectContent);

    }

ScreenShot: enter image description here

Upvotes: 0

Views: 16534

Answers (1)

Chris Haas
Chris Haas

Reputation: 55417

Do not add content in OnStartPage, instead set variables in that and write them in OnEndPage. This is in the iText book but its hard to find online documentation beyond newsgroups that say this. See this and this

EDIT

@Satinder singh, that's a lot of code to read, if you can, always try to distill it down to the smallest amount of code that still breaks and remove any of your domain-specific stuff such as session and global variables. That said, I didn't read most of it but I think I spotted your problem:

PdfPTable table2 = new PdfPTable(3);
table2.TotalWidth = document.PageSize.Width - document.LeftMargin;
table2.AddCell("cell Value 1");
table2.AddCell("Cell Value 2 ");
table2.WriteSelectedRows(0, -1, document.LeftMargin + 200, document.PageSize.Height - 30, writer.DirectContent);

Above, you're creating a three column table but you're only add two cells to it. iTextSharp is a little picky about having the correct number of cells so either change it to a two column table or add a third cell.

Upvotes: 2

Related Questions