【问题标题】:Set fixed number of rows per page, PDF generation using itextsharp设置每页的固定行数,使用 itextsharp 生成 PDF
【发布时间】:2012-10-13 07:37:08
【问题描述】:

我有一个List<object>,这个列表包含数千条记录。我想使用 itextsharp 生成 pdf。和Pdfptable 生成的 pdf 它工作正常,但我希望 pdf 中每页只有 10 条记录。
我该怎么做?

【问题讨论】:

  • 如何从该列表中检索(最多)10 个项目,使用这些项目创建一个表,将表添加到文档中,然后继续到下一页?还是我误解了你的问题?
  • 是的。这工作正常,但我的表格只覆盖了 pdf 页面的一半,另一半保持空白,我希望 10 行表格将覆盖整个 pdf 页面.. 怎么做..?

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


【解决方案1】:

在最新版本的 iTextSharp (5.3.3) 中,添加了允许您定义断点的新功能:SetBreakPoints(int[] breakPoints) 如果你定义一个 10 的倍数的数组,你可以使用它来获得想要的效果。

如果您有旧版本,您应该遍历列表并为每 10 个对象创建一个新的 PdfPTable。请注意,如果您想保持应用程序的内存使用率较低,这是更好的解决方案。

【讨论】:

  • 是的。第二个选项对我来说很好......这很好用,但我的表格只覆盖了 pdf 页面的一半,另一半保持空白,我希望 10 行的表格覆盖整个 pdf 页面.. 怎么做..?
  • 我不确定你的意思。有一种方法可以扩展最后一行,但是最后一行会有很多空白。还有一种设置固定行高的方法,但是如果内容多于行高,放不下的内容就会丢失。最后,还有一种设置最小高度的方法(您可以使用页面高度/10),但是如果内容更多,则行将超过最小高度,并且表格将被拆分并分布在两页上。您的需求描述不足以让人们能够回答。
【解决方案2】:

另一种设置每页行数的方法:

using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace RowsCountSample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var pdfDoc = new Document(PageSize.A4))
            {
                var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));
                pdfDoc.Open();

                var table1 = new PdfPTable(3);
                table1.HeaderRows = 2;
                table1.FooterRows = 1;

                //header row 
                var headerCell = new PdfPCell(new Phrase("header"));
                headerCell.Colspan = 3;
                headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
                table1.AddCell(headerCell);

                //footer row 
                var footerCell = new PdfPCell(new Phrase("footer"));
                footerCell.Colspan = 3;
                footerCell.HorizontalAlignment = Element.ALIGN_CENTER;
                table1.AddCell(footerCell);

                //adding some rows 
                for (int i = 0; i < 70; i++)
                {
                    //adds a new row
                    table1.AddCell(new Phrase("Cell[0], Row[" + i + "]"));
                    table1.AddCell(new Phrase("Cell[1], Row[" + i + "]"));
                    table1.AddCell(new Phrase("Cell[2], Row[" + i + "]"));

                    //sets the number of rows per page
                    if (i > 0 && table1.Rows.Count % 7 == 0)
                    {
                        pdfDoc.Add(table1);
                        table1.DeleteBodyRows();
                        pdfDoc.NewPage();
                    }
                }

                pdfDoc.Add(table1);
            }

            //open the final file with adobe reader for instance. 
            Process.Start("Test.pdf");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 2021-11-11
    • 2011-06-29
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    相关资源
    最近更新 更多