iTextSharp - 将Colspan与PdfPRow一起使用 [英] iTextSharp - Use Colspan with PdfPRow

查看:286
本文介绍了iTextSharp - 将Colspan与PdfPRow一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果列数相同,我可以创建多行:

I am able to create multiple rows if they contain the same number of columns:

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);

这样可以很好地给我两行,每行有三列。

This works fine giving me two rows each with three columns.

但是,如果我希望第一行只使用一个使用Colspan的长列,它就会消失:

However if I want the first row to just have one long column using Colspan it disappears:

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);

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

There are no errors given it just simply does not render.

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

Additionally, I am aware of table.AddCell which automatically starts a new row when the table column limit is reached for the current row. However, I want to use PdfPRow if at all possible.

任何帮助都将不胜感激。

Any help would be greatly appreciated.

推荐答案

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

Seems like you've read documentation that isn't endorsed by the original iText developer (being me). May I ask you for the URL where you've found that documentation, so that I can ask for a cease and desist?

关于你的问题的答案:请看看官方文档,你会发现 MyFirstTable 示例。基于此示例,您可以按如下方式调整自己的代码:

As for the answer to your question: please take a look at the official documentation, and you'll find the MyFirstTable example. Based on this example, you can adapt your own code as follows:

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 上课。 iText在内部使用 PdfPRow 类,但正如我的书中所述,使用iText的开发人员不应在其代码中使用该类。

As you can see, there's no reason to use the PdfPRow class. The PdfPRow class is used internally by iText, but as documented in my book, developers using iText shouldn't use that class in their code.

这篇关于iTextSharp - 将Colspan与PdfPRow一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆