如何在Itextsharp中添加当前页码和总页数 [英] how to add the current page number and total number of pages in Itextsharp

查看:1633
本文介绍了如何在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项目和示例中进行了解释:





#2:使用占位符表示总页数



您可以创建 PdfTemplate 作为总页数的占位符。然后,在您关闭()文档之前,您可以填写该占位符的总页数。



之前已经多次解释过:





请浏览官方文档


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.

这篇关于如何在Itextsharp中添加当前页码和总页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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