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

C# 使用iTextsharp创建PDF时在页脚添加pdfpTable,c#,asp.net,pdf,pdf-generation,itextsharp,C#,Asp.net,Pdf,Pdf Generation,Itextsharp,在我的Asp.net应用程序中,我必须生成PDF,因此我使用免费开源的Itextsharp 4.0来实现这一点,我在生成的PDF上引用set hearder/footer 但现在我的问题是我无法在页脚内添加pdfPtable,有没有建议如何在页脚插入表格 加载页面上的代码 CreatePDF("Invoice", dtaddproduct, "Prince", "1313", "04/09/2013","4000"); 在我调用的CreatePDF方法上 TwoColumnHeaderFoo

在我的Asp.net应用程序中,我必须生成
PDF
,因此我使用免费开源的Itextsharp 4.0来实现这一点,我在生成的PDF上引用set hearder/footer

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

加载页面上的代码

CreatePDF("Invoice", dtaddproduct, "Prince", "1313", "04/09/2013","4000");
在我调用的CreatePDF方法上

 TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter();
  writer.PageEvent = PageEventHandler;
document.Open()之前

自定义HeaderFooter代码(TwoColumnHeaderFooter):

编辑的代码(TwoColumnHeaderFooter):

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

截图:

不要在起始页的
中添加内容,而是在其中设置变量,并将其写入
OneDPage
。这是在iText书中,但除了说这句话的新闻组之外,很难找到在线文档。看到和

编辑

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

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

上面,您正在创建一个三列表,但只向其中添加两个单元格。ITEXTS夏普对于拥有正确的单元格数量有点挑剔,所以要么将其更改为两列表,要么添加第三个单元格。

hass:感谢您的答复,我在您所说的
onedpage
上写时尝试过,但最近它没有添加一个不使用pageEvents的答案-
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);

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