在第二次通过时使用iTextSharp将页码添加到pdf不起作用

时间:2015-07-22 11:27:15

标签: itextsharp edit

因为标题说我正在尝试将页码添加到现有的pdf文档中,所以我已经研究了这个问题,并提出了一些教程/示例。

Here is the simplest example i can find

我的代码编译并成功运行,但更改未反映在pdf

我的代码 byte[] bytes = File.ReadAllBytes(filePath + ".pdf");

        PdfReader pdfReader = new PdfReader(bytes);

        using (MemoryStream ms = new MemoryStream()) 
        {

            using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'\0',true))
            {
                int n = pdfReader.NumberOfPages;
                for (int i = 1; i <= n; i++)
                {

                    creatPageCountFooter(i + 1, n).WriteSelectedRows(0, -1, 34, 803, stamper.GetOverContent(i));

                    //ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);

                }
                ms.ToArray();
            }
            pdfReader.Close();
        }
        File.WriteAllBytes(filePath + ".pdf", bytes);

PdfReader pdfReader = new PdfReader(bytes); using (MemoryStream ms = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'\0',true)) { int n = pdfReader.NumberOfPages; for (int i = 1; i <= n; i++) { creatPageCountFooter(i + 1, n).WriteSelectedRows(0, -1, 34, 803, stamper.GetOverContent(i)); //ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0); } ms.ToArray(); } pdfReader.Close(); } File.WriteAllBytes(filePath + ".pdf", bytes);

功能&#34; creatPageCountFooter&#34;

/** * Create a header table with page X of Y * @param count the page number * @param Total the total number of pages * @return a table that can be used as header */ protected PdfPTable creatPageCountFooter(int count,int Total) { PdfPTable pageCount = new PdfPTable(3); pageCount.TotalWidth=250;

    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 6);

    PdfPCell Cell = new PdfPCell(new Phrase(DateTime.Now.ToString("dd MMM yyyy"), times));
    Cell.HorizontalAlignment = Element.ALIGN_RIGHT;
    Cell.Border = 0;
    pageCount.AddCell(Cell);

    Cell = new PdfPCell(new Phrase(count.ToString() +" / "+ Total.ToString(), times));
    Cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
    Cell.Border = 0;
    pageCount.AddCell(Cell);

    Cell = new PdfPCell(new Phrase("Company name " + DateTime.Now.ToString("yyyy"), times));
    Cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
    Cell.Border = 0;
    pageCount.AddCell(Cell);

    return pageCount;

}

作为进一步的说明,我已经检查过这段代码实际上已经运行了,我已经尝试在现有文档上编写文件或者创建一个新文档,并且两次更改都没有反映出来。

如果需要,我会提供进一步的细节。

1 个答案:

答案 0 :(得分:0)

您的代码将byte[] bytes写入文件:

File.WriteAllBytes(filePath + ".pdf", bytes);

但是设置该变量的唯一代码是初始

byte[] bytes = File.ReadAllBytes(filePath + ".pdf");

因此,更改未反映在pdf 结果文件中并不奇怪,因为您只是将原始字节写为未更改。

我假设您打算将bytes设置为此行中内存流的内容

ms.ToArray();

但忘了bytes =。不幸的是,这个数组检索调用发生得太早,它仍然在using PdfStamper块内:

using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'\0',true))
{
    ...
    ms.ToArray();
}

就像你引用的示例一样,必须在using块之外检索数组,以便在处理期间隐式关闭PdfStamper之后检索它。因此:

using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'\0',true))
{
    ...
}
bytes = ms.ToArray();