Please give me any solution; I am using this code:
HeaderFooter header = new HeaderFooter(new Phrase("This is a header"), false);
document.Header = header;
but this error occured:
CS0246:
The type or namespace name 'HeaderFooter' could not be found (are you missing a using directive or an assembly reference?
That code was deprecated and removed many years ago but still lives on in comments in the source code unfortunately.
What you want to do is subclass the iTextSharp.text.pdf.PdfPageEventHelper
class and handle the OnEndPage
method which will get called once for every page in your document:
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();
}
}
To use this you just bind a new instance of it to your PdfWriter
's PageEvent
property:
writer.PageEvent = new MyPageEventHandler();
Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.2.0 that shows this:
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.