如何在Itextsharp中添加当前页码和总页数 [英] how to add the current page number and total number of pages in Itextsharp
问题描述
pdf文件中的当前页码和总页数如何
页数:3/10
我的代码如下
// PdfPTable saleTable = SaleTable();
FileStream fileStream = new FileStream(Customer +Invoice.pdf,
FileMode.Create,
FileAccess.Write,
FileShare.None);
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(doc,fileStream);
doc.Open();
glue = new Chunk(new VerticalPositionMark());
_phrase1.Add(new Chunk(glue));
_phrase1.Add(new Chunk(Page Number:));
_para.Add(_phrase1);
doc.Add(_para);
获取当前页码很简单。你有一个名为 writer
的 PdfWriter
实例。您可以询问该实例的当前页码:
int pageNo = writer.PageNumber
在Java中:
int pageNo = writer.getPageNumber();
除非您可以展望未来,否则无法获得总页数。当您在第1页时,iText无法知道您将添加多少页面。也许你会立即调用 Close()
方法,在这种情况下总页数是1.也许你打算增加一百页。 / p>
有两种方法可以解决此问题。
#1:创建两个PDF传递
首先在内存中创建PDF而不使用页码。然后,使用 PdfStamper
添加页码。这在以下Q& A项目和示例中进行了解释:
- 示例:将页码添加到现有PDF (Java)
- 示例: TwoPasses ( Java)/ TwoPasses (C#)
- iText - 将内容添加到现有页面的底部(StackOverflow Q& A)
- iText Java - 将标题添加到现有pdf (StackOverflow Q& A)
- 在使用iTextSharp创建PDF时输入页码(StackOverflow Q& A)
- ...
#2:使用占位符表示总页数
您可以创建 PdfTemplate
作为总页数的占位符。然后,在您关闭()
文档之前,您可以填写该占位符的总页数。
之前已经多次解释过:
- 示例: MovieCountries1 (Java)/ MovieCountries1 (C#)
- Q& A section :页面活动
- 使用iTextSharp创建PDF时输入页码(StackOverflow Q& A)
- Y问题的第X页( StackOverflow Q& A)
- ...
请浏览官方文档。
how the current page number and the total number of pages in the pdf file as Page : 3/10
My code is as follows
//PdfPTable saleTable = SaleTable();
FileStream fileStream = new FileStream(Customer + "Invoice.pdf",
FileMode.Create,
FileAccess.Write,
FileShare.None);
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(doc, fileStream);
doc.Open();
glue = new Chunk(new VerticalPositionMark());
_phrase1.Add(new Chunk(glue));
_phrase1.Add(new Chunk("Page Number: "));
_para.Add(_phrase1);
doc.Add(_para);
Getting the current page number is easy. You have a PdfWriter
instance named writer
. You can ask that instance for the current page number:
int pageNo = writer.PageNumber
In Java:
int pageNo = writer.getPageNumber();
Getting the total number of pages is impossible unless you can look into the future. When you're on page 1, there is no way for iText to know how many pages you will add. Maybe you're going to invoke the Close()
method immediately, in which case the total number of pages is 1. Maybe you're planning to add a hundred pages.
There are two ways to work around this problem.
#1: create the PDF in two passes
You first create the PDF in memory without page numbers. Afterwards, you use PdfStamper
to add page numbers. This is explain in the following Q&A items and examples:
- Example: Adding page numbers to an existing PDF (Java)
- Example: TwoPasses (Java) / TwoPasses (C#)
- iText - add content to the bottom of an existing page (StackOverflow Q&A)
- iText Java - add header to an existing pdf (StackOverflow Q&A)
- put page number when create PDF with iTextSharp (StackOverflow Q&A)
- ...
#2: Use a placeholder for the total number of pages
You can create a PdfTemplate
as a place holder for the total number of pages. Then, right before you Close()
the document, you can fill out the total number of pages on that place holder.
This also has been explained many times before:
- Example: MovieCountries1 (Java) / MovieCountries1 (C#)
- Q&A section: page events
- put page number when create PDF with iTextSharp (StackOverflow Q&A)
- Page X of Y issue (StackOverflow Q&A)
- ...
Please browse the official documentation before posting a question.
这篇关于如何在Itextsharp中添加当前页码和总页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!