在第二次使用 iTextSharp 将页码添加到 pdf 不起作用
Adding page numbers to pdf using iTextSharp on second pass is not working
正如标题所说,我正在尝试将页码添加到现有的 pdf 文档中,我研究了这个问题并提出了一些 tutorials/examples。
Here is the simplest example i can find
我的代码编译并成功运行,但更改未反映在 pdf 中
我的代码
byte[] bytes = File.ReadAllBytes(filePath + ".pdf");</p>
<pre><code> PdfReader pdfReader = new PdfReader(bytes);
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'[=10=]',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);
函数"creatPageCountFooter"
/**
* 创建一个 header table 页面 X of Y
* @param 计算页码
* @param Total 总页数
* @return 可以用作 header 的 table
*/
protected PdfPTable creatPageCountFooter(int count,int Total)
{
PdfPTable pageCount = new PdfPTable(3);
pageCount.TotalWidth=250;</p>
<pre><code> 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;
}
作为进一步说明,我已经检查过此代码是否实际运行,并且我已尝试在现有文档上写入文件或创建一个新文档,但两次更改都没有反映出来。
如果需要,我会提供更多详细信息。
您的代码将 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,'[=13=]',true))
{
...
ms.ToArray();
}
就像您引用的示例一样,必须在 using
块之外检索数组,以便在 PdfStamper
处理期间隐式关闭后检索数组。因此:
using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'[=14=]',true))
{
...
}
bytes = ms.ToArray();
正如标题所说,我正在尝试将页码添加到现有的 pdf 文档中,我研究了这个问题并提出了一些 tutorials/examples。
Here is the simplest example i can find
我的代码编译并成功运行,但更改未反映在 pdf 中
我的代码
byte[] bytes = File.ReadAllBytes(filePath + ".pdf");</p>
<pre><code> PdfReader pdfReader = new PdfReader(bytes);
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'[=10=]',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);
函数"creatPageCountFooter"
/**
* 创建一个 header table 页面 X of Y
* @param 计算页码
* @param Total 总页数
* @return 可以用作 header 的 table
*/
protected PdfPTable creatPageCountFooter(int count,int Total)
{
PdfPTable pageCount = new PdfPTable(3);
pageCount.TotalWidth=250;</p>
<pre><code> 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;
}
作为进一步说明,我已经检查过此代码是否实际运行,并且我已尝试在现有文档上写入文件或创建一个新文档,但两次更改都没有反映出来。
如果需要,我会提供更多详细信息。
您的代码将 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,'[=13=]',true))
{
...
ms.ToArray();
}
就像您引用的示例一样,必须在 using
块之外检索数组,以便在 PdfStamper
处理期间隐式关闭后检索数组。因此:
using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'[=14=]',true))
{
...
}
bytes = ms.ToArray();