Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
206 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...