SelectPdf仅在自定义页面上显示页脚

时间:2019-08-28 07:16:04

标签: c# html asp.net selectpdf

我正在使用SelectPDF将HTML模板转换为PDF。我要遵循的设计要求仅在第一页上显示页脚。

documentation不覆盖自定义页面的显示。

有人尝试过吗?

1 个答案:

答案 0 :(得分:0)

如您所见,您可以创建自定义HTML文档,其名称为footer.html。本文档将包含您的自定义页脚。

这是初始化。

string footerUrl = Server.MapPath("~/files/footer.html");

之后,您必须初始化转换器对象并进行设置。

HtmlToPdf converter = new HtmlToPdf();
converter.Options.DisplayFooter = true;
converter.Footer.DisplayOnFirstPage = true;

PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);
            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);

更新:

这段代码从URL创建一个新的pdf,并为首页添加自定义页脚。

PdfDocument doc = converter.ConvertUrl(@"https://en.wikipedia.org/wiki/Chernobyl_disaster");
            PdfPage page = doc.Pages[0];

            PdfTemplate customFooter = doc.AddTemplate(
                page.PageSize.Width, 20);
            PdfHtmlElement customHtml = new PdfHtmlElement(
                "<div><b>This is the custom footer that will " +
                "appear only on page 1!</b></div>",
                string.Empty);
            customFooter.Add(customHtml);

            page.CustomFooter = customFooter;


            doc.Save("Test.pdf");
            doc.Close();

我希望它能对您有所帮助 干杯