Debananda
Debananda

Reputation: 486

Error while adding a `PdfPTable` to a document

I am facing with the

Index was outside the bounds of the array

error while adding PdfPTable to document using iTextShrap. Below is the code

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

I am having no clue on what is going wrong. Please suggest

Upvotes: 0

Views: 1707

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77576

The problem is caused by you not respecting what is explained in the documentation, more specifically the part where I wrote:

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)

If you do not even take the time to take a look at the basic examples, you should not expect that you can write code that works.

Please take a look at the SimpleTable3 example. In this example, I don't use PdfPRow. Instead I add the PdfPCell objects straight to the 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);

You don't have to worry about rows: iText creates PdfPRow instances internally (as documented).

This is what the result looks like: simple_table3.pdf

enter image description here

No exception is thrown.

Upvotes: 1

Related Questions