使用 itext 而不是仅在第一页向 PDF 的每一页添加文本
Adding text to each page of PDF with itext instead of just the first page
我正在尝试使用此代码向 PDF 的每一页添加文本,效果很好,但此代码仅用于向第一页添加文本:
//variables
String pathin = @"C:\Users\root\Desktop\temp\test.pdf";
String pathout = @"C:\Users\root\Desktop\temp\test2.pdf";
//create a document object
//var doc = new Document(PageSize.A4);
//create PdfReader object to read from the existing document
PdfReader reader = new PdfReader(pathin);
//create PdfStamper object to write to get the pages from reader
PdfStamper stamper=new PdfStamper(reader, new FileStream(pathout, FileMode.Create));
// PdfContentByte from stamper to add content to the pages over the original content
PdfContentByte pbover = stamper.GetOverContent(1);
//add content to the page using ColumnText
ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase("Hello World"), 10, 10, 0);
// PdfContentByte from stamper to add content to the pages under the original content
//PdfContentByte pbunder = stamper.GetUnderContent(1);
//close the stamper
stamper.Close();
我看过示例使用:
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var importedPage = writer.GetImportedPage(reader, i);
遍历每个页面,但我无法将其绑定到上面的代码中。任何帮助将不胜感激,谢谢。
在您的第一个代码段中,您对页码进行了硬编码:
stamper.GetUnderContent(1);
在你的第二个片段中,你遍历了页面:
for (var i = 1; i <= reader.NumberOfPages; i++) {
}
现在合并这两个片段:
for (var i = 1; i <= reader.NumberOfPages; i++) {
PdfContentByte pbunder = stamper.GetUnderContent(i);
// do stuff with bunder
}
我正在尝试使用此代码向 PDF 的每一页添加文本,效果很好,但此代码仅用于向第一页添加文本:
//variables
String pathin = @"C:\Users\root\Desktop\temp\test.pdf";
String pathout = @"C:\Users\root\Desktop\temp\test2.pdf";
//create a document object
//var doc = new Document(PageSize.A4);
//create PdfReader object to read from the existing document
PdfReader reader = new PdfReader(pathin);
//create PdfStamper object to write to get the pages from reader
PdfStamper stamper=new PdfStamper(reader, new FileStream(pathout, FileMode.Create));
// PdfContentByte from stamper to add content to the pages over the original content
PdfContentByte pbover = stamper.GetOverContent(1);
//add content to the page using ColumnText
ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase("Hello World"), 10, 10, 0);
// PdfContentByte from stamper to add content to the pages under the original content
//PdfContentByte pbunder = stamper.GetUnderContent(1);
//close the stamper
stamper.Close();
我看过示例使用:
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var importedPage = writer.GetImportedPage(reader, i);
遍历每个页面,但我无法将其绑定到上面的代码中。任何帮助将不胜感激,谢谢。
在您的第一个代码段中,您对页码进行了硬编码:
stamper.GetUnderContent(1);
在你的第二个片段中,你遍历了页面:
for (var i = 1; i <= reader.NumberOfPages; i++) {
}
现在合并这两个片段:
for (var i = 1; i <= reader.NumberOfPages; i++) {
PdfContentByte pbunder = stamper.GetUnderContent(i);
// do stuff with bunder
}