那个代码是deprecated and removed many years ago,但不幸的是仍然存在于源代码中的 cmets 中。
您要做的是继承iTextSharp.text.pdf.PdfPageEventHelper 类并处理OnEndPage 方法,该方法将为文档中的每一页调用一次:
public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper {
public override void OnEndPage(PdfWriter writer, Document document) {
//Create a simple ColumnText object
var CT = new ColumnText(writer.DirectContent);
//Bind it to the top of the document but take up the entire page width
CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height);
//Add some text
CT.AddText(new Phrase("This is a test"));
//Draw our ColumnText object
CT.Go();
}
}
要使用它,您只需将它的一个新实例绑定到您的 PdfWriter 的 PageEvent 属性:
writer.PageEvent = new MyPageEventHandler();
以下是针对 iTextSharp 5.1.2.0 的完整工作 C# 2010 WinForms 应用程序,它显示了这一点:
using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
//Test file to create
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
//Standard PDF file stream creation
using (FileStream output = new FileStream(outputFile, FileMode.Create,FileAccess.Write,FileShare.None)){
using (Document document = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(document, output)) {
//Bind our custom event handler to the PdfWriter
writer.PageEvent = new MyPageEventHandler();
//Open our PDF for writing
document.Open();
//Add some text to page 1
document.Add(new Paragraph("This is page 1"));
//Add a new page
document.NewPage();
//Add some text to page 2
document.Add(new Paragraph("This is page 2"));
//Close the PDF
document.Close();
}
}
}
this.Close();
}
}
public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper {
public override void OnEndPage(PdfWriter writer, Document document) {
//Create a simple ColumnText object
var CT = new ColumnText(writer.DirectContent);
//Bind it to the top of the document but take up the entire page width
CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height);
//Add some text
CT.AddText(new Phrase("This is a test"));
//Draw our ColumnText object
CT.Go();
}
}
}