Create a table in a generated PDF

旧城冷巷雨未停 提交于 2019-12-13 04:35:24

问题


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.

Can someone help?


回答1:


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.



来源:https://stackoverflow.com/questions/20016630/create-a-table-in-a-generated-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!