【问题标题】:Itextsharp Insert footer on end of the pageItextsharp 在页面末尾插入页脚
【发布时间】:2015-11-20 14:26:49
【问题描述】:

我有一个模板分为 3 个文件(页眉、正文和页脚),所有这些文件都在 html 中。我需要使用

导出 pdf 文件

itextsharp 库。

我使用以下代码来做到这一点。

导出文件的功能

public string GeneratePDF(Dictionary<string, object> fieldValues, string pdfPath, string templatePath)
        {
            //Inicializes a New Document
            Document document = new Document(PageSize.A4,0,0,0,0);

            try
            {
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));                 
                PdfPageEvents events = new PdfPageEvents();
                //Initializes page events
                writer.PageEvent = events;
                //Open Document
                document.Open();

                //Gerar efetivamente o html
                TemplateHelper objTemplate = new TemplateHelper();
                //This function is not important, only replaces the content by a dictionary
                string htmlContent = objTemplate.GenerateHTML(fieldValues, templatePath);

                //Gerar o PD
                StringReader reader = new StringReader(htmlContent);
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                document.Close(); 
            }

            return pdfPath;
        }

和页面事件

public class PdfPageEvents : PdfPageEventHelper
{
    public override void OnStartPage(PdfWriter writer, Document document)
    {
            StreamReader template = new StreamReader(@"d:\Header.html");
            string htmlContent = template.ReadToEnd();
            StringReader reader = new StringReader(htmlContent);

            ElementList e = XMLWorkerHelper.ParseToElementList(htmlContent, "");
            PdfDiv div = (PdfDiv)e.First();
            document.Add(div.Content.First());
            template.Close();

        template = new StreamReader(@"d:\Footer.html");
        htmlContent = template.ReadToEnd();
        reader = new StringReader(htmlContent);

    }
    //começa com o cabeçalho
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        StreamReader template = new StreamReader(@"d:\Footer.html");
        string htmlContent = template.ReadToEnd();
        StringReader reader = new StringReader(htmlContent);
        ElementList elementListFooter = XMLWorkerHelper.ParseToElementList(htmlContent, "");
        PdfDiv div = (PdfDiv)elementListFooter.First();
        PdfPTable t = (PdfPTable)div.Content.First();

        document.Add(t);
        template.Close();
    }
}

当我导出为 pdf 时,页眉工作正常,但页脚不工作。我试图将页脚内容放在

的页脚上

文件,但不成功。如果正文的内容太大而无法容纳一页,则设置页脚内容

在页眉下方和最后一页的内容下方。 下图说明了这个问题。

【问题讨论】:

  • 不要onStartPage()方法中添加任何内容。这可能导致各种不良副作用。这条评论并没有解决你的问题,但是你在onStartPage()方法中添加了header这一事实证明你忽略了文档。
  • 不要使用document.Add()onEndPage()中添加内容。传递给事件的document 对象是一个特殊的PdfDocument 对象,应该仅用于只读目的。这就是导致您的问题的原因。这也证明你忽略了文档。
  • 不相关的旁注,DocumentPdfWriter 都实现了IDisposable,因此您可以在它们上使用using 范例而不是try/catch,并且您将得到保证@987654341 @ 在 dispose 期间会自动为您调用。
  • 此外:您一遍又一遍地解析页眉和页脚的 HTML,浪费了大量的 CPU。为什么不创建一次页眉和页脚表,然后将相同的表对象作为 Form XObject 添加到每个页面。这将导致 PDF 文件的大小要小得多。

标签: c# pdf pdf-generation itextsharp


【解决方案1】:

为了更详细地解释布鲁诺在 cmets 中所说的内容,如果您在页面事件中Add() 内容,您可能会导致文档生成一个新页面,这会导致您的页面事件再次触发。更有趣的是,如果您的页面事件添加了太多内容,您实际上可能会陷入添加、溢出、新页面、重复的无限循环。

如果您想要一个真正的页眉和页脚,请设置您的Document 边距以考虑两者的高度,正常的Document.Add() 代码会尊重它。如果您不知道每次页眉和/或页脚的高度,您可以尝试使用this code to calculate the height

然后您可以使用页面事件或两次通过DirectContentColumnText 添加您的页眉和页脚以及已知的固定位置。您的内容看起来相当简单,因此您可以将其转换为普通的 iTextSharp 命令,但如果您想将页眉和页脚保留在 HTML 中,请参阅 this post 将 HTML 解析为元素列表并将其添加到 ColumnText

【讨论】:

猜你喜欢
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多