在生成的PDF中创建表 [英] Create a table in a generated PDF
问题描述
如何使用 iTextSharp
在生成的 PDF
中创建表格?
How to create a table in a generated PDF
using iTextSharp
?
我一直在尝试使用 iTextSharp
动态创建一个包含表格的PDF文档。搜索完之后,我发现了一些帖子,这些帖子对我有一个基本的帮助。但是,我想要一个具有以下结构的表格,我正面临挑战。
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.
有人可以帮忙吗?
推荐答案
我在iText网站上添加了一个小的Java示例,展示了它是如何完成的: SimpleRowColspan
I've added a small Java example to the iText site showing how it's done: SimpleRowColspan
如您所见,结果看起来非常像您描述的表格。当你说你的搜索没有找到帮助你的例子时,我不确定你的意思。也许您正在寻找一个C#示例而不是Java示例?也许你找到了我写的书附带的例子, MyFirstTable ,但未找到其C#对方。
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.
虽然我不是C#开发人员,但看起来像这样:
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.
这篇关于在生成的PDF中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!