iTextSharp:通过计算行来手动对表进行分页,但如果一行换行呢



我正在使用iTextSharp创建pdf。此pdf包含一个表,我正在使用DirectContent创建该表。我对所写的行进行计数,并在行数达到45时执行document.NewPage()。一切似乎都很好,除非任何一行换行到第二行。如果发生这种情况,我的行计数将被关闭,最终我的分页将不正确。有人知道我该如何解决这个问题吗?我的完整程序如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using static iTextSharp.text.Font;
namespace iTextSharp_long_table
{
class Program
{
static void Main(string[] args)
{
MemoryStream stream = new MemoryStream();
using (stream)
using (Document document = new Document(PageSize.LETTER))
{
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
PdfPTable table = new PdfPTable(4);
table.TotalWidth = 72 * 7.5f;
int nrows = 0;
int topOfPage = 0;
Font font = new Font(FontFamily.HELVETICA, 9, Font.BOLD);
Font fontPlain = new Font(FontFamily.HELVETICA, 9);
for (int i = 0; i < 20 ; i++)    // jobs
{
if (nrows >= 45)
{
table.WriteSelectedRows(topOfPage, -1, 36, 9 * 72, writer.DirectContent);
document.NewPage();
topOfPage = table.Size;
nrows = 0;
}
PdfPCell cell2 = new PdfPCell(new Phrase("Job " + (i + 1).ToString(), font));
cell2.Border = PdfPCell.TOP_BORDER + PdfPCell.LEFT_BORDER + PdfPCell.BOTTOM_BORDER;
cell2.BackgroundColor = new BaseColor(227, 235, 247);
cell2.Colspan = 3;
table.AddCell(cell2);
string currency = string.Format("{0:C}", Math.Round(1.23, 2));
cell2 = new PdfPCell(new Phrase(currency, font));
cell2.BackgroundColor = new BaseColor(227, 235, 247);
cell2.HorizontalAlignment = Element.ALIGN_RIGHT;
cell2.Border = PdfPCell.TOP_BORDER + PdfPCell.RIGHT_BORDER + PdfPCell.BOTTOM_BORDER;
table.AddCell(cell2);
nrows++;
// workitems
DoWorkItems(ref table, ref nrows, ref topOfPage, document, ref writer, ref fontPlain);
// add a blank row before the next job
cell2 = new PdfPCell(new Phrase(" "));
cell2.Colspan = 4;
cell2.Border = PdfPCell.NO_BORDER;
table.AddCell(cell2);
nrows++;
}
table.WriteSelectedRows(topOfPage, -1, 36, 9 * 72, writer.DirectContent);
document.NewPage();
}
byte[] result = stream.GetBuffer();
File.WriteAllBytes(@"c:tempLong Table.pdf", result);
Process.Start(@"c:tempLong Table.pdf");
}
public static void DoWorkItems(ref PdfPTable table, ref int nrows, ref int topOfPage, 
Document document, ref PdfWriter writer, ref Font fontPlain)
{
for (int j = 0; j < 5 ; j++)     // workitems
{
if (nrows >= 45)
{
table.WriteSelectedRows(topOfPage, -1, 36, 9 * 72, writer.DirectContent);
document.NewPage();
topOfPage = table.Size;
nrows = 0;
}
string str = "     - Item " + (j + 1).ToString();
Phrase phrase = new Phrase(str, fontPlain);
PdfPCell cell2 = new PdfPCell(phrase);
cell2.Colspan = 4;
cell2.Border = PdfPCell.NO_BORDER;
table.AddCell(cell2);
nrows++;
}
}
}
}

通常,最好将布局细节留给iText,特别是对于可能分布在页面上也可能不分布在页面之间的主要内容。通常创建布局的唯一内容是页眉、页脚、背景和类似材料。

在手头的案例中,评论中指出,OP亲自处理布局细节的原因是:

我这样做是因为我想在页面上的特定点开始表格(在实际程序中,我在页面顶部绘制一个图像(。

但在让iText处理布局时,这也是可能的:只需设置页边空白,使剩余空间,即iText用于布局内容的空间,成为希望显示内容的空间。

(注意:您必须在创建有问题的页面之前设置页边距。如果是第一页,这尤其意味着您必须在打开文档之前这样做。(

这样做解决了OP的问题:

由于我不必使用直接内容,我可以将表添加到文档中,并让iTextSharp进行分页。问题已解决:-(。

相关内容

最新更新