简体   繁体   English

使用iTextSharp底部的PDF页脚

[英]PDF footer at the bottom using iTextSharp

I am trying to create a pdf document in c# using iTextSharp 5.0.6. 我正在尝试使用iTextSharp 5.0.6在c#中创建pdf文档。 I want to add header and footer to every page in OnStartPage and OnEndPage events respectively. 我想分别向OnStartPage和OnEndPage事件中的每个页面添加页眉和页脚。

In case of footer there is a problem that the footer is created right where the page ends whereas I would like to be at the bottom of page. 如果使用页脚,则存在一个问题,即页脚恰好在页面结尾处创建,而我希望位于页面底部。

Is there a way in iTextSharp to specify page height so that footer is always created at the bottom. iTextSharp中是否可以指定页面高度,以便始终在底部创建页脚。

Thanks! 谢谢!

The page's height is always defined: 页面的高度始终被定义:

document.PageSize.Height // document.getPageSize().getHeight() in Java

Keep in mind that in PDF 0,0 is the lower left corner, and coordinates increase as you go right and UP. 请记住,在PDF中0,0是左下角,并且坐标随着向右和向上移动而增加。

Within a PdfPageEvent you need to use absolute coordinates. 在PdfPageEvent中,您需要使用绝对坐标。 It sounds like you're either getting the current Y from the document, or Just Drawing Stuff at the current location. 听起来您是从文档中获取当前的Y,还是在当前位置获取了Just Drawing Stuff。 Don't do that. 不要那样做

Also, if you want to use the same exact footer on every page, you can draw everything into a PdfTemplate, then draw that template into the various pages on which you want it. 另外,如果要在每个页面上使用完全相同的页脚,则可以将所有内容绘制到PdfTemplate中,然后将该模板绘制到所需的各个页面中。

PdfTemplate footerTmpl = writer.getDirectContent().createTemplate( 0, 0, pageWidth, footerHeight );

footerTmpl.setFontAndSize( someFont, someSize );
footerTmpl.setTextMatrix( x, y );
footer.showText("blah");
// etc

Then in your PdfPageEvent , you can just add footerTempl at the bottom of your page: 然后在您的PdfPageEvent ,您只需在页面底部添加footerTempl即可:

 writer.getDirectContent().addTemplateSimple( footerTmpl, 0, 0 );

Even if most of your footer is the same, you can use this technique to save memory, execution time, and file size. 即使您的大多数页脚相同,也可以使用此技术节省内存,执行时间和文件大小。

Furthermore, if you don't want to mess with PdfContentByte drawing commands directly, you can avoid them to some extent via ColumnText . 此外,如果您不想直接弄乱PdfContentByte绘制命令,则可以通过ColumnText某种程度上避免它们。 There are several SO questions tagged with iText or iTextSharp dealing with that class. 有几个用该类的iText或iTextSharp标记的SO问题。 Poke around, you'll find them. 戳一戳,您会找到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM