我使用的是iTextSharp版本5.4.5.0。
我正在尝试用多个PdfPTable打印PdfPCell。PdfPCell的数量将是动态的。那么,如何为动态生成的PdfPCell分配宽度呢?
我知道如何为单元格的静态和固定数量分配宽度。但是对于动态单元格,我如何为每个动态生成的单元分配宽度?PdfPCell的数量不是固定的。
请帮帮我?
谢谢。
发布于 2016-02-22 15:46:43
即使在对原来的问题作了反复的评论之后,我也不能完全确定我是否正确地理解了这个问题,但让我们试试:
因此,让我们假设您事先不知道列的数量,而是需要获取第一行的单元格来了解列的数量及其宽度。在这种情况下,您可以简单地执行以下操作:
public void CreatePdfWithDynamicTable()
{
using (FileStream output = new FileStream(@"test-results\content\dynamicTable.pdf", FileMode.Create, FileAccess.Write))
using (Document document = new Document(PageSize.A4))
{
PdfWriter writer = PdfWriter.GetInstance(document, output);
document.Open();
PdfPTable table = null;
List<PdfPCell> cells = new List<PdfPCell>();
List<float> widths = new List<float>();
for (int row = 1; row < 10; row++)
{
// retrieve the cells of the next row and put them into the list "cells"
...
// if this is the first row, determine the widths of these cells and put them into the list "widths"
...
// Now create the table (if it is not yet created)
if (table == null)
{
table = new PdfPTable(widths.Count);
table.SetWidths(widths.ToArray());
}
// Fill the table row
foreach (PdfPCell cell in cells)
table.AddCell(cell);
cells.Clear();
}
document.Add(table);
}
}https://stackoverflow.com/questions/35550776
复制相似问题