在 VB.NET 中使用 iTextSharp 添加标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2459165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding headers with iTextSharp in VB.NET
提问by CResults
I'm wondering how can I put a header into my PDF file, cause I've tried the tutorials from here:
我想知道如何将标题放入我的 PDF 文件中,因为我已经尝试过这里的教程:
http://itextsharp.sourceforge.net/tutorial/ch04.html
http://itextsharp.sourceforge.net/tutorial/ch04.html
And it has not worked.
它没有奏效。
I've done this:
我已经这样做了:
Dim head As New HeaderFooter(New Phrase("This is page: "), False)
head.Border = Rectangle.NO_BORDER
document.Header = head
But VS2008 says that HeaderFooter
is not defined (line 1), and Footer it's not a member of "iTextSharp.text.document" (line 3).
但是 VS2008 说HeaderFooter
没有定义(第 1 行),并且Footer it's not a member of "iTextSharp.text.document" (line 3).
I've already included the imports at the beginning of my code and iIdon't have any other problems with the iTextsharps (I mean that it is working apart of the header problem):
我已经在代码的开头包含了导入,并且 iTextsharps 没有任何其他问题(我的意思是它可以解决标题问题):
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Data.SQLite
Imports System.IO
So please, can anyone explain to me how can i set a header for my pages?
所以拜托,谁能向我解释如何为我的页面设置标题?
Regards
问候
回答by CResults
The answer to this question depends on which version of the iTextSharp dll you are using.
这个问题的答案取决于您使用的 iTextSharp dll 版本。
If you are using a version lower than 5, this should work
如果您使用的是低于 5 的版本,这应该可以工作
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Module Module1
Sub Main()
Dim pdfWrite As PdfWriter
Dim pdfDoc As New Document()
Dim pdfMemoryStream As New IO.FileStream("tryme.pdf", IO.FileMode.Create)
pdfWrite = PdfWriter.GetInstance(pdfDoc, pdfMemoryStream)
Dim pdfHeader As New HeaderFooter(New Phrase("Im at the head: "), False)
pdfHeader.Border = Rectangle.NO_BORDER
pdfDoc.Header = pdfHeader
pdfDoc.Open()
pdfDoc.Add(New Paragraph("Hello World"))
pdfDoc.NewPage()
pdfDoc.Add(New Paragraph("Hello World Again"))
pdfDoc.Close()
End Sub
End Module
Update
更新
For version 5+ of iTextSharp the HeaderFooter property has been removed from iTextSharp. To add Headers/Footers now you must use PageEvents. The following code demonstrates how to do this (very simply!)
对于 iTextSharp 5+ 版本,HeaderFooter 属性已从 iTextSharp 中删除。现在要添加页眉/页脚,您必须使用 PageEvents。以下代码演示了如何执行此操作(非常简单!)
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