如何添加一个表作为一个标题?
我一起工作的 iTextSharp的想的页眉和页脚添加到我的生成的PDF,但如果我尝试添加有我的网页宽度为100%,我有一些问题的一个标题。
I am working with iTextSharp trying to add an header and a footer to my generated PDF but, if I try to add an header that have width of 100% of my page I have some problem.
所以,我必须做以下事情:
So I have do the following things:
1)我有创建一个名为类的 PdfHeaderFooter ,它扩展了iTextSharp的 PdfPageEventHelper 类
1) I have create a class named PdfHeaderFooter that extends the iTextSharp PdfPageEventHelper class
2)进入 PdfHeaderFooter 我已经实现产生头在的OnStartPage()方式:
2) Into PdfHeaderFooter I have implemented the OnStartPage() method that generate the header:
// write on start of each page
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
PdfPTable tabHead = new PdfPTable(new float[] { 1F });
PdfPCell cell;
//tabHead.TotalWidth = 300F;
tabHead.WidthPercentage = 100;
cell = new PdfPCell(new Phrase("Header"));
tabHead.AddCell(cell);
tabHead.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
}
如果我用sometning例如 tabHead.TotalWidth = 300F; insted的 tabHead.WidthPercentage = 100; 它的工作很好,但如果我尝试设置为100%的宽度在tabHead表(如我在previous例如做),当它调用 tabHead.WriteSelectedRows(0,-1,150,document.Top,writer.DirectContent)方式扔以下例外:
If I use sometning like tabHead.TotalWidth = 300F; insted tabHead.WidthPercentage = 100; it work well but if I try to set as 100% the width of the tabHead table (as I do in the previous example) when it call the tabHead.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent) method it throw the following exception:
表格的宽度必须大于零。
为什么呢?问题是什么?这怎么可能,该表有0的大小,如果我将它设置为100%?
Why? What is the problem? How is it possible that the table have 0 size if I am setting it to 100%?
有人可以帮我解决这个问题?
Someone can help me to solve this issue?
TNX
在使用 writeSelectedRows()
,它没有意义的设置宽度百分比为100% 。设置宽度百分比意味着,当你使用添加文档 document.add()
(这是你不能在一个页面事件中使用的方法)。当使用 document.add()
,iText的计算表的基础上,页面大小和边缘宽度。
When using writeSelectedRows()
, it doesn't make sense to set the width percentage to 100%. Setting the width percentage is meant for when you add a document using document.add()
(which is a method you can't use in a page event). When using document.add()
, iText calculates the width of the table based on the page size and the margins.
您正在使用 writeSelectedRows()
,这意味着你有责任来定义的大小和表的坐标。
You are using writeSelectedRows()
, which means you are responsible to define the size and the coordinates of the table.
如果您希望表跨越页的整个宽度,您需要:
If you want the table to span the complete width of the page, you need:
table.TotalWidth = document.Right - document.Left;
您还使用了错误的X坐标:你应该使用 document.Left
而不是 150
。
You're also using the wrong X-coordinate: you should use document.Left
instead of 150
.
其他信息:
- 前两个参数定义起始行和结束行。在你的情况,你行0,这是第一行开始,你不定义结束行(这是-1表示),在这种情况下所有行绘制。
- 您省略参数列(还有的
writeSelectedRows()
即预计7参数的变化)。 - 接下来你有X和开始Y值坐标表。
- 最后,你传递一个
PdfContentByte
实例。这是在画布上,你正在绘制的表格。
- The first two parameters define the start row and the end row. In your case, you start with row 0 which is the first row, and you don't define an end row (that's what -1 means) in which case all rows are drawn.
- You omitted the parameters for the columns (there's a variation of the
writeSelectedRows()
that expects 7 parameters). - Next you have the X and Y value of start coordinate for the table.
- Finally, you pass a
PdfContentByte
instance. This is the canvas on which you're drawing the table.