如何使用Itextsharp在每页的页脚上获取pdf页面的总页数 [英] How to get total page count of pdf pages on footer on each page using Itextsharp

查看:617
本文介绍了如何使用Itextsharp在每页的页脚上获取pdf页面的总页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何使用itextsharp获取pdf文档的总页数?



我想在每页的页脚上显示总页数。

有属性document.PageCount(但是从这里我们可以设置为用户想要的数量)。

从下面的编码我得到OnCloseDocument()的总数PdfPageEventHelper的事件,但没有得到OnEndPage()的计数。



Hi,

How do I get the total page count of a document of the pdf using itextsharp?

I want to display total pagecount on footer of each page.
There is the property document.PageCount (but from this we can just set to count as user want).
From the following coding I got total count in OnCloseDocument() event of PdfPageEventHelper but not getting this count on OnEndPage().

int totcountPage = 0;
public override void OnCloseDocument(PdfWriter writer, Document document)
{
       total.BeginText();
       int pageNumber = writer.PageNumber - 1;
       total.ShowText(Convert.ToString(pageNumber));
       total.EndText();
       totcountPage = writer.PageNumber;
    //MessageBox.Show(p.ToString());        
} 





在上面我有总页数,但我想显示页面的页脚。



In above I have got total count of pages but I want to display on footer of page.

int i=0;
public override void OnEndPage(PdfWriter writer, Document doc)
{  
   Paragraph para = new Paragraph(i + " of "  + totcountPage + "", footer);
  doc.Add(para);
}





这里的事件OnEndPage()首先像之前一样调用OnCloseDocument(),这就是为什么将totcountPage显示为0的原因。所以我没有得到我想要的正确输出。



前显示输出

1 of 0 ...在第一页

5 of 0 ...在第五页



预期产量

1 of 5 ...开第一页

5 of 5 ...在第五页



所以请帮助我。



Here as event OnEndPage() called first as before of OnCloseDocument() , thats why displaying totcountPage as 0. so I am not getting right output which I want.

For ex showing output
1 of 0... On first page
5 of 0...On Fifth Page

For ex expected output
1 of 5... On first page
5 of 5 ...On Fifth Page

So please help me.

推荐答案

我看到它的方式,你将有两个选择:



1)构建你的完整PDF然后添加你的页脚,但我不知道ItextSharp是否可行。



2)创建一个内存流,构建你的PDF,获取总页数,然后创建一个文件流(re使用正确的脚注构建PDF。
The way I see it, you'll have 2 options:

1) Build your complete PDF and then add your footers, but I don't know if that's possible with ItextSharp.

2) Create a memorystream, build your PDF, get the total number of pages and then create a filestream that (re)builds your PDF with the correct footnotes.


http://nilthakkar.blogspot.com/2013/11/itextsharpadd-headerfooter-to-pdf.html [ ^ ]


我不知道。我认为这是不可能的。因为直到文件完成,你才能知道你会得到多少页。解释?
I have no idea. I think it is not possible .Because until the document is finished that can you know how many pages you will get .Understand?


这篇关于如何使用Itextsharp在每页的页脚上获取pdf页面的总页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆