在 iText7 (.NET) 中为现有 PDF 的每一页添加页脚
Add footer to every page of an existing PDF in iText7 (.NET)
我正在 itext7.pdfHTML 中使用模板文件在运行时构建 PDF。我想为生成的 PDF 的每一页添加一个页脚,它有两页,但由于某种原因,页脚只出现在第二页上。
我正在尝试从 iText 网站构建 this example。该示例涉及页码,但由于我只是将静态文本添加到我的文档中,所以原理是相同的。这是我的代码:
string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";
//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));
HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);
addFooter(id);
}
//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
Document doc = new Document(pdfDoc);
Paragraph foot = new Paragraph(footer);
foot.SetFontSize(8);
float x = 300; //559
float y = 0; //806
int numberOfPages = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
doc.ShowTextAligned(foot, x, y, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
}
doc.Close();
//delete temporary PDF
File.Delete(destination);
}
我试过在 addFooter()“for”循环中将 i 设置为 0,但这并没有解决问题。如何让页脚出现在每一页上?
是的,您没有指定要将页脚添加到哪个页面,所以它只将其添加到整个文档的底部。试试这个:
请注意,唯一的变化是:doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";
//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));
HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);
addFooter(id);
}
//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
Document doc = new Document(pdfDoc);
Paragraph foot = new Paragraph(footer);
foot.SetFontSize(8);
float x = 300; //559
float y = 0; //806
int numberOfPages = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
}
doc.Close();
//delete temporary PDF
File.Delete(destination);
}
我正在 itext7.pdfHTML 中使用模板文件在运行时构建 PDF。我想为生成的 PDF 的每一页添加一个页脚,它有两页,但由于某种原因,页脚只出现在第二页上。
我正在尝试从 iText 网站构建 this example。该示例涉及页码,但由于我只是将静态文本添加到我的文档中,所以原理是相同的。这是我的代码:
string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";
//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));
HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);
addFooter(id);
}
//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
Document doc = new Document(pdfDoc);
Paragraph foot = new Paragraph(footer);
foot.SetFontSize(8);
float x = 300; //559
float y = 0; //806
int numberOfPages = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
doc.ShowTextAligned(foot, x, y, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
}
doc.Close();
//delete temporary PDF
File.Delete(destination);
}
我试过在 addFooter()“for”循环中将 i 设置为 0,但这并没有解决问题。如何让页脚出现在每一页上?
是的,您没有指定要将页脚添加到哪个页面,所以它只将其添加到整个文档的底部。试试这个:
请注意,唯一的变化是:doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";
//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));
HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);
addFooter(id);
}
//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");
PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
Document doc = new Document(pdfDoc);
Paragraph foot = new Paragraph(footer);
foot.SetFontSize(8);
float x = 300; //559
float y = 0; //806
int numberOfPages = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
}
doc.Close();
//delete temporary PDF
File.Delete(destination);
}