Y问题的第X页

Bra*_*rad 13 itextsharp

我尝试了3种不同的方式来显示页码, OnCloseDocument内容没有显示在页面中,没有一个工作.

我的目的是显示页码这样

1 of 10
2 0f 10
..............
............
每页10的10

我知道如何展示

1
2
3
4
....
10

不知道如何显示总页数

我使用 OnCloseDocument 来显示页数计数,但其中的内容未显示.

public class MyPdfPageEventHelpPageNo : iTextSharp.text.pdf.PdfPageEventHelper
{
    protected PdfTemplate total;
    protected BaseFont helv;
    private bool settingFont = false;

    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        template= writer.DirectContent.CreateTemplate(100, 100);       

        bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    }
    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
    //See below
    }
Run Code Online (Sandbox Code Playgroud)

第一种方式:

    public override void OnCloseDocument(PdfWriter writer, Document document)     
    {
        //I create a table with one column like below.
        PdfPTable pageNumber2 = new PdfPTable(1);
        pageNumber2.TotalWidth = 50;
        pageNumber2.HorizontalAlignment = Element.ALIGN_RIGHT;

        pageNumber2.AddCell(BuildTable2RightCells("Page " + writer.PageNumber));
        pageNumber.AddCell(BuildTable2LeftCells(writer.PageCount));
        pageNumber2.WriteSelectedRows(0, -1, 500, 
        (document.PageSize.GetBottom(140)), cb);
    }    
Run Code Online (Sandbox Code Playgroud)

第二种方式:

    public override void OnCloseDocument(PdfWriter writer, Document document)     
    {
        ColumnText.ShowTextAligned(template,Element.ALIGN_CENTER,new
        Phrase(writer.PageNumber.ToString()), 500, 140, 0);
    }
Run Code Online (Sandbox Code Playgroud)

3RD方式:

    public override void OnCloseDocument(PdfWriter writer, Document document)     
    {
        template.BeginText();
        template.SetFontAndSize(bf, 8);
        template.SetTextMatrix(500, 140);
        template.ShowText(Convert.ToString((writer.PageNumber - 1)));
        template.EndText();
    }
Run Code Online (Sandbox Code Playgroud)

我做错了吗?

Chr*_*aas 18

你的第二种方式可能是最简单的方法.下面是一个非常非常纤薄但工作的版本:

public class MyPdfPageEventHelpPageNo : iTextSharp.text.pdf.PdfPageEventHelper {
    public override void OnEndPage(PdfWriter writer, Document document) {
        ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase(writer.PageNumber.ToString()), 500, 140, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用它:

//Create a file on our desktop
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "OnCloseTest.pdf");
//Standard PDF creation, adjust as needed
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (Document doc = new Document(PageSize.LETTER)) {
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {

            //Find our custom event handler
            writer.PageEvent = new MyPdfPageEventHelpPageNo();

            doc.Open();

            //Add text the first page
            doc.Add(new Paragraph("Test"));

            //Add a new page with more text
            doc.NewPage();
            doc.Add(new Paragraph("Another Test"));

            doc.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

对不起,我以为您在事件的基本设置方面遇到了问题,我的错误.

我只看到了两种方法来做你想要做的事情,要么做两次传递,要么使用PdfTemplate语法,据我所知渲染图像.

我建议只运行两次传递,第一次只是创建你的PDF,第二次是添加你的页码.你可以运行你的第一个传递给MemoryStream你,所以如果你愿意,你不必两次击中磁盘.

PdfReader reader = new PdfReader(outputFile);
using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (PdfStamper stamper = new PdfStamper(reader, fs)) {
        int PageCount = reader.NumberOfPages;
        for (int i = 1; i <= PageCount; i++) {
            ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_CENTER, new Phrase(String.Format("Page {0} of {1}", i, PageCount)), 500, 140, 0);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)