gpt4 book ai didi

iTextSharp 缺少 HeaderFooter 类

转载 作者:行者123 更新时间:2023-12-04 00:59:33 25 4
gpt4 key购买 nike

这很奇怪,我目前正在使用 iTextSharp,我想在我的 PDF 中添加页眉和页脚。在所有示例中,他们只是创建了一个新的 HeaderFooter() 对象。但是,我导入了所有 iTextSharp 库,但未定义 HeaderFooter。我已经使用 Reflector 来查看我是否可以找到该类的下落以及它是否丢失了?!

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

最佳答案

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


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

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

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 支持的任何内容。

关于iTextSharp 缺少 HeaderFooter 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2598917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com