带有页眉和页脚文本的itextsharp多页表

时间:2016-11-04 15:42:43

标签: c# pdf itext

我正在尝试创建一个pdf生成器,在页面的顶部和底部有一些文本,中间有一个表。该表设置为适合的行长度。一切正常,除了PdfPTable表保留以前添加的行的值,所以当我输出pdf时,它保留了最后一个表。有没有办法添加具有相同变量的新表或清除当前变量?

            doc.Open();

            Paragraph header = new Paragraph("header");
            header.Alignment = Element.ALIGN_CENTER;
            Paragraph footer = new Paragraph("footer");
            footer.Alignment = Element.ALIGN_CENTER;

            PdfPTable table = new PdfPTable(3);
            table.LockedWidth = true;
            table.SetWidths(widths);
            table.HorizontalAlignment = 0;

            foreach (T t in results)
            {
                if (counter % 50 == 0)
                {
                    if (counter != 0)
                    {
                        doc.Add(table);
                        doc.Add(footer);
                        doc.NewPage();
                    }
                    doc.Add(header);
                    table.AddCell("Name"); //Table Header
                    table.AddCell("Address"); //Table Header
                    table.AddCell("Phone"); //Table Header

                }
                    table.AddCell("First Last"); //individual cell from t.name
                    table.AddCell("Address"); //individual cell from t.address
                    table.AddCell("Phone"); //individual cell from t.phone
            }
            doc.Add(table);
            doc.Add(footer);
            doc.Close();

1 个答案:

答案 0 :(得分:1)

我只需在if语句table = new PdfPTable(5);

中添加if (counter != 0)就可以解决这个问题。
相关问题