iTextSharp - 将Colspan与PdfPRow一起使用

Bax*_*ter 5 pdf-generation rows itextsharp 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);
Run Code Online (Sandbox Code Playgroud)

这很好,给我两行,每行三列.

在此输入图像描述

但是,如果我希望第一行只使用一个使用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);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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

任何帮助将不胜感激.

Bru*_*gie 16

好像你已经阅读了原始iText开发人员(不是我)认可的文档.我可以问你找到那份文件的URL,以便我可以要求停止和终止吗?

至于你的问题的答案:请查看官方文档,你会找到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");
Run Code Online (Sandbox Code Playgroud)

如您所见,没有理由使用该PdfPRow课程.该PdfPRow班由iText的内部使用,但在我的书中记载,利用iText不应该使用这个类在他们的代码的开发人员.