[英]Set fixed number of rows per page, PDF generation using itextsharp
I have a List<object>
and this list contains thousands of record. 我有一个
List<object>
,此列表包含数千条记录。 I want to generated pdf using itextsharp. 我想使用itextsharp生成pdf。 and
Pdfptable
to generated pdf it is working fine, but but I want only 10 records per page in pdf. 和
Pdfptable
生成pdf可以正常工作,但是我只希望pdf中每页10条记录。
How can I do it? 我该怎么做?
Another way to set the number of rows per page: 设置每页行数的另一种方法:
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");
}
}
}
In the most recent version of iTextSharp (5.3.3), new functionality was added allowing you to define breakpoints: SetBreakPoints(int[] breakPoints)
If you define an array of multiples of 10, you can use this to get the desired effect. 在最新版本的iTextSharp(5.3.3)中,添加了新功能,可让您定义断点:
SetBreakPoints(int[] breakPoints)
如果定义10的倍数数组,则可以使用此数组获得所需的效果。
If you have an older version, you should loop over the list and create a new PdfPTable for every 10 objects. 如果您使用的是较旧的版本,则应遍历列表并为每10个对象创建一个新的PdfPTable。 Note that this is the better solution if you want to keep the memory use of your application low.
请注意,如果要降低应用程序的内存使用量,这是更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.