4

我已经使用iText库动态创建了一个PDF文件,现在我想在 PDF 的页面中添加页眉页脚,为此我添加了给定的代码:

document.addHeader("My Header Title", "My Header Details");

但在我的 PDF 页面中,无法设置此标题。什么是我不知道的问题,如果您对此有任何想法,请分享您的想法。

4

4 回答 4

11

如果您使用当前的 iText 版本(即现在的 5.4.x),请查看iText in Action — 2nd Edition中的示例MovieHistory2,它显示了如何将标题(奇数页和偶数页的不同标题)添加到 PDF 同时创建它。

最重要的是PdfPageEventHelper实现

/** Inner class to add a header and a footer. */
class HeaderFooter extends PdfPageEventHelper {
    /** Alternating phrase for the header. */
    Phrase[] header = new Phrase[2];
    /** Current page number (will be reset for every chapter). */
    int pagenumber;

    /**
     * Initialize one of the headers.
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
     *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
     */
    public void onOpenDocument(PdfWriter writer, Document document) {
        header[0] = new Phrase("Movie history");
    }

    /**
     * Initialize one of the headers, based on the chapter title;
     * reset the page number.
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onChapter(
     *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document, float,
     *      com.itextpdf.text.Paragraph)
     */
    public void onChapter(PdfWriter writer, Document document,
            float paragraphPosition, Paragraph title) {
        header[1] = new Phrase(title.getContent());
        pagenumber = 1;
    }

    /**
     * Increase the page number.
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(
     *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
     */
    public void onStartPage(PdfWriter writer, Document document) {
        pagenumber++;
    }

    /**
     * Adds the header and the footer.
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
     *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
     */
    public void onEndPage(PdfWriter writer, Document document) {
        Rectangle rect = writer.getBoxSize("art");
        switch(writer.getPageNumber() % 2) {
        case 0:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_RIGHT, header[0],
                    rect.getRight(), rect.getTop(), 0);
            break;
        case 1:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_LEFT, header[1],
                    rect.getLeft(), rect.getTop(), 0);
            break;
        }
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
                (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
    }
}

这是这样注册的:

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    HeaderFooter event = new HeaderFooter();
    writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
    writer.setPageEvent(event);

编辑:根据评论中的要求,该onEndPage方法的一个更简单的变体带有静态标头而不是交替标头:

    public void onEndPage(PdfWriter writer, Document document)
    {
        Rectangle rect = writer.getBoxSize("art");
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_RIGHT, new Phrase("My static header text"),
                rect.getRight(), rect.getTop(), 0);
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
                (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
    }
于 2013-06-20T15:10:59.037 回答
2

请参考这个网站.......

https://www.coderanch.com/how-to/java/ItextExample

于 2013-06-20T13:23:29.820 回答
2

请首先参考这个问题的公认答案。
这个答案非常有帮助(它帮助了我)。
以防万一您使用 C# 进行编程,这里是相同的接受答案,但在 C# 版本中

/// <summary>
/// Inner class to add a header and a footer.
/// </summary>
internal class HeaderFooter : PdfPageEventHelper
{
    private Phrase[] header = new Phrase[2];
    private int pageNumber;

    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        header[0] = new Phrase("Smares in Header");
    }

    public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
    {
        header[1] = new Phrase(title.Content);
        pageNumber = 1;
    }

    public override void OnStartPage(PdfWriter writer, Document document)
    {
        pageNumber++;
    }

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        Rectangle rect = writer.GetBoxSize("art");
        switch (writer.PageNumber % 2)
        {
            case 0:
                ColumnText.ShowTextAligned(writer.DirectContent,
                        Element.ALIGN_RIGHT, header[0],
                        rect.Right, rect.Top, 0);
                break;
            case 1:
                ColumnText.ShowTextAligned(writer.DirectContent,
                        Element.ALIGN_LEFT, header[1],
                        rect.Left, rect.Top, 0);
                break;
        }

        ColumnText.ShowTextAligned(writer.DirectContent,
                Element.ALIGN_CENTER, new Phrase(String.Format("page {0}", pageNumber)),
                (rect.Left + rect.Right) / 2, rect.Bottom - 18, 0);
    }
}

活动的注册将是:

using (MemoryStream ms = new MemoryStream())
{
    using (Document doc = new Document(PageSize.A4, -30, -30, 45, 45))
    {
        using (PdfWriter writer = PdfWriter.GetInstance(doc, ms))
        {
             HeaderFooter ev = new HeaderFooter();
             writer.SetBoxSize("art", new Rectangle(36, 54, 559, 788));
             writer.PageEvent = ev;

             // continue your code here 
        }
    }
}

注意:这只是将接受的答案从 java 转换为 C#。但是您可以根据自己的需要自定义它,就像我对它所做的那样。

于 2016-06-23T09:56:33.383 回答
-3

你可以这样添加

HeaderFooter header = new HeaderFooter(new Phrase("Add Header Part Here"), false);  
HeaderFooter footer = new HeaderFooter(new Phrase("Add Footer Here"), new Phrase("."));  

document.setHeader(header);  
document.setFooter(footer); 
于 2013-06-20T13:24:56.017 回答