使用iTextsharp创建PDF时,在页脚添加pdfpTable

时间:2013-04-22 10:48:10

标签: c# asp.net pdf pdf-generation itextsharp

在我的Asp.net应用程序中,我必须生成PDF所以我使用 Itextsharp 4.0 这是免费的开源,我引用this来设置听众/页脚在生成的PDF上,

但现在我的问题是我无法在页脚中添加 pdfPtable ,是否有任何建议如何在页脚插入表格?

代码Page_Load

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

CreatePDF 方法中,我正在调用

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

之前document.Open();

自定义HeaderFooter代码(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();
    }

已编辑的代码(TwoColumnHeaderFooter):

我编辑放在OnEndPage上的代码。在这里我遇到同样的问题,看看截图,你会发现一个链接更多信息正在显示,而不是我也用代码编写的表。 帮助请

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);

    }

截图: enter image description here

1 个答案:

答案 0 :(得分:2)

请勿在{{1​​}}中添加内容,而是在其中设置变量并将其写入OnStartPage。这是在iText的书中,但很难找到新闻组之外的在线文档。请参阅thisthis

修改

@Satinder singh,这是很多要阅读的代码,如果可以的话,总是尝试将其提炼到仍然会破坏并删除任何特定于域的内容(如会话和全局变量)的最小代码量。也就是说,我没有读过大部分内容,但我认为我发现了你的问题:

OnEndPage

上面,您正在创建一个三列表,但您只需添加两个单元格。 iTextSharp对于拥有正确数量的单元格有点挑剔,因此要么将其更改为两列表格,要么添加第三个单元格。