将 `PdfPTable` 添加到文档时出错
Error while adding a `PdfPTable` to a document
我正面临
Index was outside the bounds of the array
使用 iTextShrap
将 PdfPTable
添加到文档时出错。下面是代码
Document myDocument = new Document(PageSize.A3.Rotate());
PdfWriter.GetInstance(myDocument, new FileStream("payslip.pdf", FileMode.Create));
myDocument.Open();
PdfPTable table = new PdfPTable(35);
table.TotalWidth = myDocument.PageSize.Width - 80f;
table.LockedWidth = true;
List<PdfPCell> headRow = new List<PdfPCell>();
PdfPCell contractor = new PdfPCell(new Phrase("XXXXXXXXXXXXX"));
contractor.Colspan = 5;
headRow.Add(contractor);
PdfPCell workType = new PdfPCell(new Phrase("Refractory Works"));
workType.Colspan = 5;
headRow.Add(workType);
PdfPCell supervisor = new PdfPCell(new Phrase("XXXXXXXXXXXXXX"));
supervisor.Colspan = 4;
headRow.Add(supervisor);
PdfPCell paySlipHead = new PdfPCell(new Phrase("XXXXXXXXXXXXXXXX"));
paySlipHead.Colspan = 10;
headRow.Add(paySlipHead);
PdfPCell paySlipMonth = new PdfPCell(new Phrase("XXXXXXX"));
paySlipMonth.Colspan = 2;
headRow.Add(paySlipMonth);
PdfPCell blank = new PdfPCell(new Phrase(""));
blank.Colspan = 9;
headRow.Add(blank);
PdfPRow rw = new PdfPRow(headRow.ToArray());
table.Rows.Add(rw);
myDocument.Add(table);
我不知道出了什么问题。请推荐
问题是由于您不遵守文档中的解释引起的,更具体地说是我写的部分:
There’s a PdfPRow
object in the com.itextpdf.text.pdf
package, but you aren’t supposed
to address it directly. iText uses this class internally to store the cells that belong
to the same row. (iText in Action - Second Edition, section 4.1.1 "Your first Table", p94)
如果您甚至不花时间看一下基本示例,就不要指望您可以编写有效的代码。
请查看 SimpleTable3 示例。在此示例中,我不使用 PdfPRow
。相反,我将 PdfPCell
对象直接添加到 PdfPTable
:
PdfPTable table = new PdfPTable(35);
table.setTotalWidth(document.getPageSize().getWidth() - 80);
table.setLockedWidth(true);
PdfPCell contractor = new PdfPCell(new Phrase("XXXXXXXXXXXXX"));
contractor.setColspan(5);
table.addCell(contractor);
PdfPCell workType = new PdfPCell(new Phrase("Refractory Works"));
workType.setColspan(5);
table.addCell(workType);
PdfPCell supervisor = new PdfPCell(new Phrase("XXXXXXXXXXXXXX"));
supervisor.setColspan(4);
table.addCell(supervisor);
PdfPCell paySlipHead = new PdfPCell(new Phrase("XXXXXXXXXXXXXXXX"));
paySlipHead.setColspan(10);
table.addCell(paySlipHead);
PdfPCell paySlipMonth = new PdfPCell(new Phrase("XXXXXXX"));
paySlipMonth.setColspan(2);
table.addCell(paySlipMonth);
PdfPCell blank = new PdfPCell(new Phrase(""));
blank.setColspan(9);
table.addCell(blank);
document.add(table);
您不必担心行:iText 在内部创建 PdfPRow
个实例(如文档所述)。
结果是这样的:simple_table3.pdf
没有抛出异常。
我正面临
Index was outside the bounds of the array
使用 iTextShrap
将 PdfPTable
添加到文档时出错。下面是代码
Document myDocument = new Document(PageSize.A3.Rotate());
PdfWriter.GetInstance(myDocument, new FileStream("payslip.pdf", FileMode.Create));
myDocument.Open();
PdfPTable table = new PdfPTable(35);
table.TotalWidth = myDocument.PageSize.Width - 80f;
table.LockedWidth = true;
List<PdfPCell> headRow = new List<PdfPCell>();
PdfPCell contractor = new PdfPCell(new Phrase("XXXXXXXXXXXXX"));
contractor.Colspan = 5;
headRow.Add(contractor);
PdfPCell workType = new PdfPCell(new Phrase("Refractory Works"));
workType.Colspan = 5;
headRow.Add(workType);
PdfPCell supervisor = new PdfPCell(new Phrase("XXXXXXXXXXXXXX"));
supervisor.Colspan = 4;
headRow.Add(supervisor);
PdfPCell paySlipHead = new PdfPCell(new Phrase("XXXXXXXXXXXXXXXX"));
paySlipHead.Colspan = 10;
headRow.Add(paySlipHead);
PdfPCell paySlipMonth = new PdfPCell(new Phrase("XXXXXXX"));
paySlipMonth.Colspan = 2;
headRow.Add(paySlipMonth);
PdfPCell blank = new PdfPCell(new Phrase(""));
blank.Colspan = 9;
headRow.Add(blank);
PdfPRow rw = new PdfPRow(headRow.ToArray());
table.Rows.Add(rw);
myDocument.Add(table);
我不知道出了什么问题。请推荐
问题是由于您不遵守文档中的解释引起的,更具体地说是我写的部分:
There’s a
PdfPRow
object in thecom.itextpdf.text.pdf
package, but you aren’t supposed to address it directly. iText uses this class internally to store the cells that belong to the same row. (iText in Action - Second Edition, section 4.1.1 "Your first Table", p94)
如果您甚至不花时间看一下基本示例,就不要指望您可以编写有效的代码。
请查看 SimpleTable3 示例。在此示例中,我不使用 PdfPRow
。相反,我将 PdfPCell
对象直接添加到 PdfPTable
:
PdfPTable table = new PdfPTable(35);
table.setTotalWidth(document.getPageSize().getWidth() - 80);
table.setLockedWidth(true);
PdfPCell contractor = new PdfPCell(new Phrase("XXXXXXXXXXXXX"));
contractor.setColspan(5);
table.addCell(contractor);
PdfPCell workType = new PdfPCell(new Phrase("Refractory Works"));
workType.setColspan(5);
table.addCell(workType);
PdfPCell supervisor = new PdfPCell(new Phrase("XXXXXXXXXXXXXX"));
supervisor.setColspan(4);
table.addCell(supervisor);
PdfPCell paySlipHead = new PdfPCell(new Phrase("XXXXXXXXXXXXXXXX"));
paySlipHead.setColspan(10);
table.addCell(paySlipHead);
PdfPCell paySlipMonth = new PdfPCell(new Phrase("XXXXXXX"));
paySlipMonth.setColspan(2);
table.addCell(paySlipMonth);
PdfPCell blank = new PdfPCell(new Phrase(""));
blank.setColspan(9);
table.addCell(blank);
document.add(table);
您不必担心行:iText 在内部创建 PdfPRow
个实例(如文档所述)。
结果是这样的:simple_table3.pdf
没有抛出异常。