当我注意到格式错误时,我正在使用iText 点网库。我能够在一个演示该问题的小项目中重现它。
每当我有一个或多个大于页面高度的网格并且我在网格上调用 SetKeepTogether(true) 时,我会得到多个表格以一种奇怪的方式重叠。当调用 SetKeepTogether(false) 时,不会发生渲染问题,但是当表格超出页面边界时,我需要表格中断到新页面。
在我的示例中,我声明了一个名为 CAUSE_BUG 的静态布尔值。当设置为 true 时,它将生成有缺陷的 PDF。当设置为 false 时,它将生成几乎不适合页面的表格,并且不会出现呈现问题。
我是否以某种方式错误地使用了该库?如果是这样,我将如何纠正这个问题?
const bool CAUSE_BUG = true;
const string DEST = "sample.pdf";
const int COL_COUNT = 5;
static void Main(string[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
PdfWriter writer = new PdfWriter(DEST);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.Default.Rotate());
document.Add(CreateTable(4));
document.Add(CreateTable(8));
if (CAUSE_BUG)
{
// The bug happens when one or more tables that are marked to keep together extend
// past the boundary of the page's height. 21 rows is just over the page height.
document.Add(CreateTable(21));
document.Add(CreateTable(21));
document.Add(CreateTable(21));
}
else
{
// These grids should barely fit in a single page which will cause them to render nicely.
document.Add(CreateTable(20));
document.Add(CreateTable(20));
document.Add(CreateTable(20));
}
document.Close();
}
private static Table CreateTable(int rows)
{
Table t = new Table(COL_COUNT);
t.SetWidthPercent(100);
Cell topCell = new Cell(1, COL_COUNT);
topCell.Add("FULL WIDTH HEADER");
topCell.SetBorder(Border.NO_BORDER);
topCell.SetBold();
t.AddHeaderCell(topCell);
Cell threeWide = new Cell(1, 3);
threeWide.Add("Three Wide");
threeWide.SetBold();
t.AddHeaderCell(threeWide);
Cell twoWide = new Cell(1, 2);
twoWide.Add("Two Wide");
twoWide.SetBold();
t.AddHeaderCell(twoWide);
// Add dummy rows
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int i = 0; i < COL_COUNT; i++)
{
t.AddCell(string.Format("R{0} C{0}", rowIndex, i));
}
t.StartNewRow();
}
// Setting this to false will fix the issue
t.SetKeepTogether(true);
return t;
}