【发布时间】:2015-06-10 17:39:56
【问题描述】:
我只想在 pdftable 的页面中显示 5 列,而在 下一页 中显示剩余的列。我们可以设置pdftable中的列数吗?请帮助我实现这一目标。
【问题讨论】:
标签: c# pdf itext pdf-generation
我只想在 pdftable 的页面中显示 5 列,而在 下一页 中显示剩余的列。我们可以设置pdftable中的列数吗?请帮助我实现这一目标。
【问题讨论】:
标签: c# pdf itext pdf-generation
请看一下这个PdfPTable constructor:
public PdfPTable(int numColumns)构造一个带有
numColumns列的 PdfPTable。参数:
- numColumns - 列数
因此,如果您想创建一个包含 5 列的 PdfPTable,您需要像这样创建您的 PdfPTable:
PdfPTable table = new PdfPTable(5);
当然,如果您已经有一个包含更多列的PdfPTable,那么您不能只使用以下行添加该表:
document.add(table);
以上行将添加所有列。
如果您只想添加包含更多列的表格的 5 列,则需要控制布局。您唯一的办法是使用writeSelectedRows() 方法。
您可以找到有关如何使用此方法的示例here:
float newY = table.writeSelectedRows(0, 5, 0, -1, 36, 806, canvas);
此方法将渲染表的前 5 列(这就是参数 0 和 5 的含义)和所有行(这就是参数 0 和 -1 的含义;@987654336 @ 表示:直到最后一行)。我们将x = 36 和y = 806 位置处的表格添加到直接内容(canvas)中。
但是:当您对表格进行完全控制时,您有责任确保表格适合页面。例如:如果不是所有行都适合页面,那么表格将超出页面的可见区域。
为避免这种情况,您应该先计算每一行的高度。
另外:如果表格没有覆盖整个页面,您必须跟踪 Y 位置。在上面的代码sn-p中,表格末尾的Y位置是newY。
任何后续的document.add() 操作将不会知道这个newY,因此重要的是要了解切换到绝对定位意味着您不能再计数在 iText 上自动进行定位。
【讨论】:
writeSelectedRows() 来定位每一页上的列。
PdfPTable。稍后根据数据创建另一个没有列的对象并分配给较早的对象。
PdfPTable,并为numColumns 参数。
==> 创建函数或方法 GenerateFile()
void GenerateFile()
{
string path = Application.dataPath + "/myfirstfile.pdf";
if (File.Exists(path)) File.Delete(path);
//Create Document
var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
var document = new Document(PageSize.A4,10f,10f,10f,0f);
var writer = PdfWriter.GetInstance(document, fileStream);
document.Open();
document.NewPage();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
//Create Table
int noOfcoloumn = 5;
PdfPTable mytable = new PdfPTable(noOfcoloumn);
mytable .AddCell("Order ID"); //Row 1 Strat
mytable .AddCell("Order Time");
mytable .AddCell("User");
mytable .AddCell("Order Status");
mytable .AddCell("View");
mytable .AddCell("OID3242"); //Row 2 Strat
mytable .AddCell("04/03/2021 14:50:51");
mytable .AddCell("Atul R Patel");
mytable .AddCell("Delivered");
mytable .AddCell("ShowOrderItem");
//Debug.Log("Total Number of Columns:-- " +table.NumberOfColumns);//[In Unity //Debuging]
document.Add(mytable); // Add in to pdf file
document.Add(Chunk.NEWLINE);
Paragraph p1 = new Paragraph(string.Format("Total Number of Columns : {0}",
mytable.NumberOfColumns ));
p1.Alignment = Element.ALIGN_CENTER;
document.Add(p1);
document.Close();
writer.Close();
【讨论】: