我使用iTextSharp在每页的现有pdf文件中添加页脚。我的代码有效,但我无法跳过第一页(我不想在页面上翻页),这是我的代码:
public void Success()
{
var guid = Guid.NewGuid();
var outPath = "~/Content/pdfs/Ebook - " + guid + ".pdf";
var mappedPathIn = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/pdfs/ebook - 1.pdf");
var mappedPathOut = System.Web.Hosting.HostingEnvironment.MapPath(outPath);
var pdfReader = new PdfReader(mappedPathIn);
var size = pdfReader.GetPageSizeWithRotation(1);
var document = new Document(size);
var fs = new FileStream(mappedPathOut, FileMode.Create, FileAccess.Write);
var writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = writer.DirectContent;
document.NewPage();
PdfImportedPage tempPage = writer.GetImportedPage(pdfReader, 1);
cb.AddTemplate(tempPage, 0, 0);
writer.PageEvent =
new PdfHelper("This is a test");
for (int pageNumber = 2; pageNumber <= pdfReader.NumberOfPages; pageNumber++)
{
document.NewPage();
PdfImportedPage page = writer.GetImportedPage(pdfReader, pageNumber);
cb.AddTemplate(page, 0, 0);
}
document.Close();
}
以下是我生成pdf的代码:
PdfContentByte cb = writer.DirectContent;
document.NewPage();
PdfImportedPage tempPage = writer.GetImportedPage(pdfReader, 1);
cb.AddTemplate(tempPage, 0, 0);
writer.PageEvent =
new PdfHelper("This is a test");
所以我真的很累,从第2页开始循环并在循环之前添加第一页,然后设置PageEvent:
Custom a= new Custom("");
Custom b= a;
//b would be equal a. because they reference the same object.
Custom c= new Custom("");
//c would not be equal to a, although the value is the same.
但这也为他的第一页添加了页脚。