当前位置: 首页 > 知识库问答 >
问题:

在iText7(.NET)中为现有PDF的每一页添加页脚

吕自明
2023-03-14

我在运行时用一个模板文件在itext7.pdfhtml中构建一个PDF。我想在生成的PDF中的每一页都添加一个页脚,该PDF有两页,但出于某种原因,页脚只出现在第二页。

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);
}

共有1个答案

庄博厚
2023-03-14

是的,您没有指定将页脚添加到哪个页面,所以它只将页脚添加到整个文档的底部。试试这个:

注意,唯一的更改是: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);
}
 类似资料:
  • 我试图创建一个带有页眉和页脚的PDF。页眉和页脚都是图像。由于我的pdf创建了随机数量的页面,我需要自动将其添加到每一页。我知道我需要使用某种eventhandler。不幸的是,我找不到vb.net语言中的任何示例,我只能找到java/C#示例,我真的不擅长阅读/转换这些语言vb.net.我还不是编程专家。 谁能给我指一下正确的方向吗。 Edit4:删除的随机内容不再需要回答我的问题。 下面这段代

  • 我可以使用iText7和C#ASP. NET创建一个pdf文件 这个pdf文件共包含9页。 我试图在代码中添加总页数和一条虚线 但我有两个问题 页数(9页中的1页,9页中的2页,9页中的3页...)只显示在PDF文件中的第8页和第9页中 虚线没有显示在第一页... 任何帮助都将不胜感激。。。非常感谢。 下面是我的代码

  • 我当前正试图添加一个链接到pdf文档的页脚页眉,但是库给出了以下错误System.IndexoutOfrangeException:“请求的页码0已超出范围。”当使用IText7库将链接添加到标头时。 我在IText7中找不到任何关于这个问题的在线代码示例,ITextSharp中的解决方案不再适用。 我的问题是我如何添加一个链接到一个外部网站到PDF的标题?当前的行为是库中的bug还是有意的? 包

  • 问题内容: 因此,我想在Linux中使用groff通过postscript生成的pdf文件的每一页底部添加一个“页脚”(属性)。我自己使用ps2pdf工具将文件从ps转换为pdf,因此可以同时使用这两种格式。 我不反对使用第一种方法,但是我无权访问第一个脚本中提到的实用程序,也没有选择将其安装在需要执行此操作的计算机上的选择。 看来第二种方法可能可行,但是我安装了ghostscript版本8.15

  • 我正在尝试向PDF文档的第一页添加一些内容。这样做的合适方式是什么? 目前,我的代码可以工作,但它会在文档的第一页之前添加(插入)一个新页面。这里可以用什么来代替 因此,我正在阅读的文档中的内容将作为内容而不是新页面添加到现有的第一页

  • 问题内容: 我有普通的PDF文件,我想使用,在PDF 的末尾插入空白页,而不会打扰PDF内容。 问题答案: Dinup Kandel的答案是错误的,因为它是关于从头开始创建文档的。 NK123的答案 非常错误, 因为它使用/ 连接文件。该示例假定原始文档中的所有页面的尺寸均为A4。并非总是如此。如记录所示,这也将丢弃所有交互性。 唯一的好答案是这样的: 如果引用的文档有10页,则上面的代码将使用与