首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用iTextSharp 5.0.2.0在pdf文件中添加页码

问使用iTextSharp 5.0.2.0在pdf文件中添加页码
EN

Stack Overflow用户
提问于 2019-09-20 15:18:28
回答 2查看 101关注 0票数 0

我想要获得使用iTextSharp 5.0.2.0生成的pdf的每一页的页码。

现在代码的一部分是

代码语言:javascript
复制
e.FooterText = writer.CurrentPageNumber.ToString();

但当我这样做时,它会显示页码,但问题是它在所有页面中都显示相同的页码。就像所有页面中的"1“。如何在iTextSharp 5.0.2.0版本中获取pdf文件的页码?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-20 15:32:44

你必须使用iTextSharp打开PDF,然后按照下面的代码进行操作。

代码语言:javascript
复制
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();
   }
}
票数 0
EN

Stack Overflow用户

发布于 2019-09-20 16:55:19

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

下面的代码示例将在每次触发OnPageEnd事件时在所需的页面位置添加所需的页脚文本

代码语言:javascript
复制
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);
    }
}

下面是一些关于如何使其工作的示例代码

代码语言:javascript
复制
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类覆盖其他页面事件,如OnStartPage等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58023553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档