Orson
Orson

Reputation: 15451

Create a table in a generated PDF

How to create a table in a generated PDF using iTextSharp?

I've have been trying to create a PDF document containing a table dynamically using iTextSharp. After search around, I found a few posts which helped me with a basic one. However, I wanted a table with the following structure and I am having a challenge creating it.

Table Structure

Can someone help?

Upvotes: 0

Views: 4725

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

I've added a small Java example to the iText site showing how it's done: SimpleRowColspan

As you can see, the result looks very much like the table you describe. I'm not sure what you mean when you say your searches didn't result in finding an example that helped you. Maybe you're looking for a C# example instead of a Java example? Maybe you found the example that accompanies the book I wrote, MyFirstTable, but didn't find its C# counterpart.

Although I'm not a C# developer, that would look like this:

PdfPTable table = new PdfPTable(5);
float[] widths = new float[] { 1f, 2f, 2f, 2f, 1f };
table.SetWidths(widths);
PdfPCell cell;
cell = new PdfPCell(new Phrase("S/N"));
cell.Rowspan = 2;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Name"));
cell.Colspan = 3;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Age"));
cell.Rowspan = 2;
table.AddCell(cell);
table.AddCell("SURNAME");
table.AddCell("FIRST NAME");
table.AddCell("MIDDLE NAME");
table.AddCell("1");
table.AddCell("James");
table.AddCell("Fish");
table.AddCell("Stone");
table.AddCell("17");

This should answer your question. If not, please clarify what isn't working for you.

Upvotes: 1

Related Questions