如何将HTML页眉和页脚添加到页面?

时间:2015-10-28 14:43:57

标签: java itext

如何使用itext从html源添加标题到pdf?

目前,我们已经扩展了PdfPageEventHelper并覆盖了这些方法。工作正常,但是当我到达2页以上时会抛出RuntimeWorkerException。

 @Override
    void onStartPage(PdfWriter writer, Document document) {
        InputStream is = new ByteArrayInputStream(header?.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
    }

    @Override
    void onEndPage(PdfWriter writer, Document document) {
        InputStream is = new ByteArrayInputStream(footer?.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);

    }

2 个答案:

答案 0 :(得分:3)

禁止一般在onStartPage()事件中添加内容。 禁止将内容添加到document中的onEndPage()对象。您应该使用onEndPage() NOT 文档在PdfWriter方法中添加标题和页脚。另外:通过一遍又一遍地解析HTML,你浪费了大量的CPU。

请查看HtmlHeaderFooter示例。

它有两个HTML代码段,一个用于页眉,一个用于页脚。

public static final String HEADER =
    "<table width=\"100%\" border=\"0\"><tr><td>Header</td><td align=\"right\">Some title</td></tr></table>";
public static final String FOOTER =
    "<table width=\"100%\" border=\"0\"><tr><td>Footer</td><td align=\"right\">Some title</td></tr></table>";

请注意,有更好的方法来描述页眉和页脚,而不是使用HTML,但也许这是你的要求之一,所以我不会问你为什么你不使用任何在the official documentation中解释的方法。顺便说一下:在free ebook中也可以找到解决问题所需的所有信息,因此您可能需要下载它...

我们只会在页面活动中阅读这些HTML代码段,然后我们会在每个页面上反复渲染这些元素:

public class HeaderFooter extends PdfPageEventHelper {
    protected ElementList header;
    protected ElementList footer;
    public HeaderFooter() throws IOException {
        header = XMLWorkerHelper.parseToElementList(HEADER, null);
        footer = XMLWorkerHelper.parseToElementList(FOOTER, null);
    }
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            ColumnText ct = new ColumnText(writer.getDirectContent());
            ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));
            for (Element e : header) {
                ct.addElement(e);
            }
            ct.go();
            ct.setSimpleColumn(new Rectangle(36, 10, 559, 32));
            for (Element e : footer) {
                ct.addElement(e);
            }
            ct.go();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
}

您是否看到我们用于添加从XML Worker获取的Element个对象的机制?我们创建了一个ColumnText对象,该对象将写入writer直接内容(禁止使用document)。我们定义了一个Rectangle,我们使用go()来渲染元素。

结果显示在html_header_footer.pdf

答案 1 :(得分:3)

Bruno的anwser是正确的,但它并没有完全对我有用,因为XMLWorkerHelper.parsetoElementsList无法解析某些系统字体另一方面XMLWorkerHelper.getInstance()。parseXHtml(writer,document,是);     我能够正确地解析系统字体,所以我必须沿着元素处理程序的路线走下去,在这里处理C#中的代码

    /// <summary>
    /// returns pdf in bytes.
    /// </summary>
    /// <param name="contentsHtml">contents.</param>
    /// <param name="headerHtml">header contents.</param>
    /// <param name="footerHtml">footer contents.</param>
    /// <returns></returns>
    public Byte[] GetPDF(string contentsHtml, string headerHtml, string footerHtml)
    {
        // Create a byte array that will eventually hold our final PDF
        Byte[] bytes;

        // Boilerplate iTextSharp setup here

        // Create a stream that we can write to, in this case a MemoryStream
        using (var ms = new MemoryStream())
        {
            // Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
            using (var document = new Document(PageSize.A4, 40, 40, 120, 120))
            {
                // Create a writer that's bound to our PDF abstraction and our stream
                using (var writer = PdfWriter.GetInstance(document, ms))
                {
                    // Open the document for writing
                    document.Open();

                    var headerElements = new HtmlElementHandler();
                    var footerElements = new HtmlElementHandler();

                    XMLWorkerHelper.GetInstance().ParseXHtml(headerElements, new StringReader(headerHtml));

                    XMLWorkerHelper.GetInstance().ParseXHtml(footerElements, new StringReader(footerHtml));

                    writer.PageEvent = new HeaderFooter(headerElements.GetElements(), footerElements.GetElements());

                    // Read your html by database or file here and store it into finalHtml e.g. a string
                    // XMLWorker also reads from a TextReader and not directly from a string
                    using (var srHtml = new StringReader(contentsHtml))
                    {
                        // Parse the HTML
                        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, srHtml);
                    }

                    document.Close();
                }
            }

            // After all of the PDF "stuff" above is done and closed but **before** we
            // close the MemoryStream, grab all of the active bytes from the stream
            bytes = ms.ToArray();
        }
        return bytes;
    }
}

页面事件和元素处理程序代码在这里

public partial class HeaderFooter : PdfPageEventHelper
{
    private ElementList HeaderElements { get; set; }
    private ElementList FooterElements { get; set; }

    public HeaderFooter(ElementList headerElements, ElementList footerElements)
    {
        HeaderElements = headerElements;
        FooterElements = footerElements;
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        try
        {
            ColumnText headerText = new ColumnText(writer.DirectContent);
            foreach (IElement e in HeaderElements)
            {
                headerText.AddElement(e);
            }
            headerText.SetSimpleColumn(document.Left, document.Top, document.Right, document.GetTop(-100), 10, Element.ALIGN_MIDDLE);
            headerText.Go();

            ColumnText footerText = new ColumnText(writer.DirectContent);
            foreach (IElement e in FooterElements)
            {
                footerText.AddElement(e);
            }
            footerText.SetSimpleColumn(document.Left, document.GetBottom(-100), document.Right, document.GetBottom(-40), 10, Element.ALIGN_MIDDLE);
            footerText.Go();
        }
        catch (DocumentException de)
        {
            throw new Exception(de.Message);
        }
    }
}

public class HtmlElementHandler : IElementHandler
{
    public ElementList Elements { get; set; }

    public HtmlElementHandler()
    {
        Elements = new ElementList();
    }

    public ElementList GetElements()
    {
        return Elements;
    }

    public void Add(IWritable w)
    {
        if (w is WritableElement)
        {
            foreach (IElement e in ((WritableElement)w).Elements())
            {
                Elements.Add(e);
            }
        }
    }
}