itext 当前页数 总页数_如何在Itextsharp中添加当前页码和总页数

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:

#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:

Please browse the official documentation before posting a question.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值