iTextSharp缺少HeaderFooter类 [英] iTextSharp is missing HeaderFooter class

查看:382
本文介绍了iTextSharp缺少HeaderFooter类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪,我目前正在使用iTextSharp,我想添加一个Header&页脚到我的PDF文件。在所有示例中,它们只是创建一个新的HeaderFooter()对象。但是,我已经导入了所有导入的iTextSharp库,但未定义HeaderFooter。我用反射器来看看我是否能找到这个课程的下落和遗漏?!

This is weird, I am currently using iTextSharp and I want to add a Header & Footer to my PDFs. In all the examples they simply create a new HeaderFooter() object. However, I have iTextSharp libraries all imported but the HeaderFooter is not defined. I've used Reflector to see if I can find out whereabouts the class is and its missing?!

有谁知道这堂课发生了什么事?

Does anyone know what has happened to this class?

推荐答案

大多数示例都是指早期版本的iTextSharp。对于iTextSharp的5+版本(我假设您正在使用),HeaderFooter属性/对象已被删除。

Most of the examples refer to an earlier version of iTextSharp. For version 5+ of iTextSharp (which I assume you are using) the HeaderFooter property/object has been removed.

请参阅
http://itextpdf.com/history/?branch=50&node=500 (最后一行)

要立即添加页眉/页脚,您必须使用PageEvents。以下代码演示了如何在VB中执行此操作。您基本上必须继承PageEventsHelper类并监视OnStartPage事件 - 然后根据需要添加您的代码。

To add Headers/Footers now you must use PageEvents. The following code demonstrates how to do this in VB. You basically have to inherit the PageEventsHelper class and watch for the OnStartPage event - then add your code as necessary.

Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Module Module1
    Sub Main()
        Dim pdfDoc As New Document()
        Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("tryme2.pdf", FileMode.Create))
        Dim ev As New itsEvents
        pdfWrite.PageEvent = ev
        pdfDoc.Open()
        pdfDoc.Add(New Paragraph("Hello World"))
        pdfDoc.NewPage()
        pdfDoc.Add(New Paragraph("Hello World Again"))
        pdfDoc.Close()
    End Sub
End Module

Public Class itsEvents
    Inherits PdfPageEventHelper
    Public Overrides Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
        Dim ch As New Chunk("This is my Stack Overflow Header on page " & writer.PageNumber)
        document.Add(ch)
    End Sub
End Class

它最初看起来更多的工作,但有一个好处,你可以添加更多的页眉/页脚,而不仅仅是纯文本。您现在可以轻松添加Document支持的任何内容。

It initially looks like more work but has the upside that you can add more to your header/footer than just plain text. You can now for example easily add anything that Document will support.

这篇关于iTextSharp缺少HeaderFooter类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆