itext 当前页数 总页数 您所在的位置:网站首页 itext7生成pdf怎么添加页码 itext 当前页数 总页数

itext 当前页数 总页数

2024-06-28 17:23| 来源: 网络整理| 查看: 265

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.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有