C# 如何使用itextsharp向每个pdf页面添加页码
这是我想要的,我想在我动态生成的每个pdf页面中添加页码 我使用了结束页的方法,但即使在添加文档底部边距时,它也不起作用 我决定在从文件路径生成pdf后添加页码。 以下是我生成pdf的代码:C# 如何使用itextsharp向每个pdf页面添加页码,c#,pdf,pdf-generation,itextsharp,C#,Pdf,Pdf Generation,Itextsharp,这是我想要的,我想在我动态生成的每个pdf页面中添加页码 我使用了结束页的方法,但即使在添加文档底部边距时,它也不起作用 我决定在从文件路径生成pdf后添加页码。 以下是我生成pdf的代码: Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("t5
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("t5.pdf", FileMode.Create));
doc.Open();//Open Document to write
iTextSharp.text.Font font8 = FontFactory.GetFont("ARIAL", 7);
Paragraph paragraph = new Paragraph("Some content");
doc.Add(paragraph);
doc.Add(paragraph);// add paragraph to the document
doc.Close();
FileStream stream = File.OpenRead("t5.pdf");
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
AddPageNumbers(fileBytes);
using (Stream file = File.OpenWrite("t5.pdf"))
{
file.Write(fileBytes, 0, fileBytes.Length);
}
}
她是我添加页码的方法:
MemoryStream ms = new MemoryStream();
PdfReader reader = new PdfReader(pdf);
int n = reader.NumberOfPages;
iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
Document document = new Document(psize, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
PdfContentByte cb = writer.DirectContent;
int p = 0;
for (int page = 1; page <= reader.NumberOfPages; page++)
{
document.NewPage();
p++;
PdfImportedPage importedPage = writer.GetImportedPage(reader, page);
cb.AddTemplate(importedPage, 0, 0);
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.BeginText();
cb.SetFontAndSize(bf, 10);
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, +p + "/" + n, 100, 450, 0);
cb.EndText();
}
document.Close();
return ms.ToArray();
MemoryStream ms=newmemoryStream();
PdfReader reader=新PdfReader(pdf);
int n=reader.NumberOfPages;
iTextSharp.text.Rectangle psize=reader.GetPageSize(1);
文件=新文件(psize,50,50,50);
PdfWriter writer=PdfWriter.GetInstance(文档,ms);
document.Open();
PdfContentByte cb=writer.DirectContent;
int p=0;
对于(int page=1;page对不起,讲座放在第一位
在此处发布问题时,请仅发布尽可能少的代码。您的“创建多页PDF示例”是116行长。其中有复杂的PdfPTable
和DataTable
逻辑,与问题100%无关。相反,以下13行足以构成一个多页PDF:
//Create a sample multiple page PDF and place it on the desktop
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "t5.pdf");
using (var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
for (var i = 0; i < 1000; i++) {
doc.Add(new Paragraph(String.Format("This is paragraph #{0}", i)));
}
doc.Close();
}
}
}
对不起,先讲课
在此处发布问题时,请仅发布尽可能少的代码。您的“创建多页PDF示例”是116行长。其中有复杂的PdfPTable
和DataTable
逻辑,与问题100%无关。相反,以下13行足以构成一个多页PDF:
//Create a sample multiple page PDF and place it on the desktop
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "t5.pdf");
using (var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
for (var i = 0; i < 1000; i++) {
doc.Add(new Paragraph(String.Format("This is paragraph #{0}", i)));
}
doc.Close();
}
}
}
Hii,@Chris Haas,当我运行上面的代码创建示例PDF时,它运行得很好,但在另一方面,当我尝试运行页码代码时,PDF没有打开。示例PDF中的字节数为3250,但页码中的PDF字节数仅为15。因此,我的代码有问题,您可以假设它为什么会这样做。Hii,@ChrHaas,当我运行上面的代码创建示例PDF时,它运行得很好,但在另一面,当我尝试运行页码代码时,PDF没有打开。示例PDF中的字节数为3250,但页码中的PDF字节数仅为15。因此,我的代码有问题,您可以假设它为什么会这样做。