在 iText 中使用分页符创建绝对定位 table
Create absolute positioned table with page breaks in iText
我试图将 table 放置在 PDF 文档的绝对位置并让 table 进入下一页,但是,将 table 添加到 ColumnText似乎阻止了这一点,我怀疑这是因为我正在使用 DirectContent 编写但我不能确定。
这是显示此内容的代码片段。
void Main(string[] args)
{
new Splitting().manipulatePdf(Splitting.dest);
}
public class Splitting {
public static string dest = @"d:\splitting.pdf";
public void manipulatePdf(String dest)
{
var doc = new Document(PageSize.A4);
var ms = new MemoryStream();
var writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.NewPage();
Paragraph p = new Paragraph("Test");
var table = new PdfPTable(2);
for (int i = 1; i < 60; i++) {
table.AddCell("key " + i);
table.AddCell("value " + i);
}
doc.Add(table);
ColumnText ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(0, 0, 300, 300);
ct.AddElement(table);
ct.Go();
doc.Close();
File.WriteAllBytes(dest, ms.ToArray());
}
}
你有这个代码:
ColumnText ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(0, 0, 300, 300);
ct.AddElement(table);
ct.Go();
这会在 300 x 300 用户单位的矩形中添加 table。 ct.Go()
的 return 值会通知您是否所有矩形都足够大以适合 table,或者 table 中是否还有一些内容不适合.在后一种情况下,您必须定义一个新列,可能在一个新页面上。
参见官方文档中的 ColumnTable 示例(这是一个 Java 示例;C# 版本的 link 可以在页面底部找到)。
ColumnText column = new ColumnText(writer.DirectContent);
float[][] x = {
new float[] { document.Left, document.Left + 380 },
new float[] { document.Right - 380, document.Right }
};
column.AddElement(GetTable(day));
int count = 0;
int status = 0;
// render the column as long as it has content
while (ColumnText.HasMoreText(status)) {
column.SetSimpleColumn(
x[count][0], document.Bottom,
x[count][1], document.Top
);
// render as much content as possible
status = column.Go();
// go to a new page if you've reached the last column
if (++count > 1) {
count = 0;
document.NewPage();
}
}
如果您坚持使用 iTextSharp 5,这就是代码。如果您升级到最新版本 iText 7 for C#,您的代码将更易于阅读。在那种情况下,这只是将 DocumentRenderer
更改为 ColumnDocumentRenderer
的情况,如 chapter 2 of the iText 7: Building Blocks 教程中所述。渲染器的概念是 iText 7 的主要改进之一。如果你希望你的代码面向未来,你应该考虑升级。
我试图将 table 放置在 PDF 文档的绝对位置并让 table 进入下一页,但是,将 table 添加到 ColumnText似乎阻止了这一点,我怀疑这是因为我正在使用 DirectContent 编写但我不能确定。
这是显示此内容的代码片段。
void Main(string[] args)
{
new Splitting().manipulatePdf(Splitting.dest);
}
public class Splitting {
public static string dest = @"d:\splitting.pdf";
public void manipulatePdf(String dest)
{
var doc = new Document(PageSize.A4);
var ms = new MemoryStream();
var writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.NewPage();
Paragraph p = new Paragraph("Test");
var table = new PdfPTable(2);
for (int i = 1; i < 60; i++) {
table.AddCell("key " + i);
table.AddCell("value " + i);
}
doc.Add(table);
ColumnText ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(0, 0, 300, 300);
ct.AddElement(table);
ct.Go();
doc.Close();
File.WriteAllBytes(dest, ms.ToArray());
}
}
你有这个代码:
ColumnText ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(0, 0, 300, 300);
ct.AddElement(table);
ct.Go();
这会在 300 x 300 用户单位的矩形中添加 table。 ct.Go()
的 return 值会通知您是否所有矩形都足够大以适合 table,或者 table 中是否还有一些内容不适合.在后一种情况下,您必须定义一个新列,可能在一个新页面上。
参见官方文档中的 ColumnTable 示例(这是一个 Java 示例;C# 版本的 link 可以在页面底部找到)。
ColumnText column = new ColumnText(writer.DirectContent);
float[][] x = {
new float[] { document.Left, document.Left + 380 },
new float[] { document.Right - 380, document.Right }
};
column.AddElement(GetTable(day));
int count = 0;
int status = 0;
// render the column as long as it has content
while (ColumnText.HasMoreText(status)) {
column.SetSimpleColumn(
x[count][0], document.Bottom,
x[count][1], document.Top
);
// render as much content as possible
status = column.Go();
// go to a new page if you've reached the last column
if (++count > 1) {
count = 0;
document.NewPage();
}
}
如果您坚持使用 iTextSharp 5,这就是代码。如果您升级到最新版本 iText 7 for C#,您的代码将更易于阅读。在那种情况下,这只是将 DocumentRenderer
更改为 ColumnDocumentRenderer
的情况,如 chapter 2 of the iText 7: Building Blocks 教程中所述。渲染器的概念是 iText 7 的主要改进之一。如果你希望你的代码面向未来,你应该考虑升级。