pdf-generation - iTextSharp - 使用 Colspan 和 PdfPRow

标签 pdf-generation itextsharp rows pdfptable

如果它们包含相同数量的列,我可以创建多行:

table = new PdfPTable(3);

var firstRowCell1 = new PdfPCell( new Phrase ("Row 1 - Column 1"));
var firstRowCell2 = new PdfPCell( new Phrase ("Row 2 - Column 2"));
var firstRowCell3 = new PdfPCell( new Phrase ("Row 3 - Column 3"));
PdfPCell[] row1Cells = { firstRowCell1, firstLineRow2, firstRowCell3 };
var row1 = new PdfPRow(row1Cells);
table.Rows.Add(row1);

var nextRowCell1 = new PdfPCell( new Phrase ("Row 2 - Column 1"));
var nextRowCell2 = new PdfPCell( new Phrase ("Row 2 - Column 2"));
var nextRowCell3 = new PdfPCell( new Phrase ("Row 2 - Column 3"));
PdfPCell[] row2Cells = { nextRowCell1, nextRowCell2, nextRowCell3 };
var row2 = new PdfPRow(row2Cells);
table.Rows.Add(row2);

这很好用,给我两行,每行三列。

enter image description here

但是,如果我希望第一行只有一个使用 Colspan 的长列,它就会消失:
var table = new PdfPTable(3);  

var firstRowCell1 = new PdfPCell(new Phrase("Row 1 - Column 1"));
firstRowCell1.Colspan = 3;
PdfPCell[] row1Cells = { firstRowCell1 };
var row1 = new PdfPRow(row1Cells);
deptHeaderTable.Rows.Add(row1);

var nextRowCell1 = new PdfPCell(new Phrase("Row 2 - Column 1"));
var nextRowCell2 = new PdfPCell(new Phrase("Row 2 - Column 2"));
var nextRowCell3 = new PdfPCell(new Phrase("Row 2 - Column 3"));
PdfPCell[] row2Cells = { nextRowCell1, nextRowCell2, nextRowCell3 };
var row2 = new PdfPRow(row2Cells);
deptHeaderTable.Rows.Add(row2);

enter image description here

没有错误,因为它只是不呈现。

此外,我知道 table.AddCell 会在当前行达到表列限制时自动开始一个新行。但是,如果可能的话,我想使用 PdfPRow。

任何帮助将不胜感激。

最佳答案

似乎您已经阅读了原始 iText 开发人员(作为我)未认可的文档。我可以问你找到该文档的 URL,以便我可以要求停止和停止吗?

至于你的问题的答案:请看the official documentation ,您会发现 MyFirstTable例子。基于此示例,您可以按如下方式调整自己的代码:

var table = new PdfPTable(3);
var cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
table.AddCell("row 2; cell 3");

如您所见,没有理由使用 PdfPRow类(class)。 PdfPRow iText 在内部使用类,但正如我在书中所记录的那样,使用 iText 的开发人员不应在其代码中使用该类。

关于pdf-generation - iTextSharp - 使用 Colspan 和 PdfPRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19864032/

相关文章:

iOS 通过创建 pdf 得到错误

pdf - 将代码跟踪到 PDF 或 PostScript 文件中

c# - itextsharp 将点转换为厘米

c# - 如何使用 iTextSharp 拼合已填写的 PDF 表单

c# - 使用 itextsharp 在 pdf 文件中画线的问题

c# - Azure Web 表单和 HTML 到 PDF 转换器

java - pdf 文件中的可扩展列表

r - 将序列中的值分配给一组连续的行,留下一些行为空

javascript - PHPExcel 搜索/返回单元格中包含特定值的所有行

javascript - 如何统计过滤后显示的表格行数?