使用 iTextSharp 5.0.2.0 的 pdf 文件中的页码

page number in pdf file using iTextSharp 5.0.2.0

我想在使用 iTextSharp 5.0.2.0 生成的 pdf 的每一页中获取页码。

现在部分代码是

e.FooterText = writer.CurrentPageNumber.ToString();

但是当我这样做时,它显示的是页码,但问题是它在所有页面中都显示相同的页码。就像所有页面中的“1”一样。如何使用 iTextSharp 5.0.2.0 版获取 pdf 文件的页码?有什么想法吗??

您必须使用 iTextSharp 打开 PDF,然后按照下面的代码来帮助您


public void AddPageNumberToPDF(string physicalDocPath, bool showPageOfPage)
{
   byte[] Fbytes = File.ReadAllBytes(physicalDocPath);
   PdfReader reader = new PdfReader(Fbytes);
   int n = reader.NumberOfPages;
   using (var fileStream = new FileStream(physicalDocPath, FileMode.Create, FileAccess.Write))
   {
       var document = new Document(reader.GetPageSizeWithRotation(1));
       var writer = PdfWriter.GetInstance(document, fileStream);
       document.Open();
       PdfContentByte cb = writer.DirectContent;
       int p = 0;

       for (int page = 1; page <= reader.NumberOfPages; page++)
       {
           document.NewPage();
           p++;

           PdfImportedPage importedPage = writer.GetImportedPage(reader, page);
           cb.AddTemplate(importedPage, 0, 0);

           BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
           cb.BeginText();
           cb.SetFontAndSize(bf, 10);
           if (showPageOfPage)
           {
               cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, p.ToString()+"/"+n.ToString(), 575, 17, 0);
           }
           else
           {
               cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, p.ToString(), 575, 17, 0);
           }
           cb.EndText();
       }

       document.Close();
       writer.Close();
   }
}

如@bradbury9 所述,OnEndPage 事件就是您的目标。

下面是一个代码示例,每次触发 OnPageEnd 事件时都会在所需的页面位置添加所需的页脚文本

public class MyPdfPageEventHandler: PdfPageEventHelper
{
    const float horizontalPosition = 0.5f;  // %50 of the page width, starting from the left
    const float verticalPosition = 0.1f;    // %10 of the page height starting from the bottom

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        var footerText = new Phrase(writer.CurrentPageNumber.ToString());

        float posX = writer.PageSize.Width * horizontalPosition;
        float posY = writer.PageSize.Height * verticalPosition;
        float rotation = 0;

        ColumnText.ShowTextAligned(writer.DirectContent, Element.PHRASE, footerText, posX, posY, rotation);
    }
}

这里有一些关于如何让它工作的示例代码

static void Main(string[] args)
{
    FileStream fs = new FileStream("NewDocument.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
    Document doc = new Document();
    PdfWriter writer = PdfWriter.GetInstance(doc, fs);
    writer.PageEvent = new MyPdfPageEventHandler(); //This will trigger the code above

    doc.Open();
    doc.Add(new Paragraph("First Page"));
    doc.NewPage();
    doc.Add(new Paragraph("Second Page"));
    doc.NewPage();
    doc.Add(new Paragraph("Thid Page"));
    doc.Close();
}

您可以使用 MyPdfPageEventHandler class 覆盖其他页面事件,例如 OnStartPage 等。