且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iTextSharp的添加页眉和页脚,而不是外部的页边距页边距内显示。

更新时间:2022-10-19 13:56:45

由于你是新来的iText ...当你正在寻找一些关于特定主题的样本,你应该先去看看进入关键词列表从的: //itextpdf.com/book/相对=nofollow>的iText在行动 - 第二版。你的情况关键字页眉/页脚是合适的。引用有第一个示例, part1.chapter05.MovieHistory2 ,已经向您介绍如何添加页眉和页脚一般。



首先,因为你已经提到你自己,你应该使用页面事件,的OnEndPage 确切的说,这最容易通过延长 PdfPageEventHelper。



此外进行,因为根据你的问题,你似乎没有意识到,你应该的不可以通过调用 document.add 或类似的东西添加页眉或页脚,因为这种补充进入这已经现已完成主页区域(我们在的OnEndPage 毕竟...)。 。相反,你应该定位使用直接访问内容的页眉和页脚



样品做这样的:

  / ** 
*添加页眉和页脚。
* @see com.itextpdf.text.pdf.PdfPageEventHelper#的OnEndPage(
* com.itextpdf.text.pdf.PdfWriter,com.itextpdf.text.Document)
* /
公共无效的OnEndPage(PdfWriter作家,文档文件){
矩形RECT = writer.getBoxSize(艺术);
开关(writer.getPageNumber()%2){
案0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT,标题[0],
rect.getRight(),rect.getTop(),0);
中断;
壳体1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT,标题[1],
rect.getLeft(),rect.getTop(), 0);
中断;
}
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER,新的短语(的String.format(%d页,页面编号)),
(RECT .getLeft()+ rect.getRight())/ 2,rect.getBottom() - 18,0);
}



Col​​umnText.showTextAligned 让你的位置的页眉,页脚等正是你希望他们在页面上,顶部和底部边缘外正常,而不会导致产生的任何新的网页或任何其他有害影响。


I am new to ItextSharp, just wondering how to place header and footer outside the margin after end of page event? it seems when i used the onendpage event instead of adding the footer outside of the page margin, it adds inside of page margin, and it always produce *** exception when it will be over the bottom margin where it should add outside of the margin?

are there any settings to tell to add the text in document footer outside of margin (or is it padding)?

thanks in advance.

As you are new to iText... when you are looking for samples concerning some specific topic, you should first go and look into the Keyword list for samples from iText in Action — 2nd Edition. In your case the keyword Header / footer is appropriate. The first sample referenced there, part1.chapter05.MovieHistory2, already shows you how to add headers and footers in general.

First of all, as you already have mentioned yourself, you should use page events, onEndPage to be exact, which most easily is done by extending PdfPageEventHelper.

Furthermore, as according to your question you seem not to be aware of, you should not add headers or footers by calling document.add or something similar because such additions go into the main page area which already is finished now (we're in onEndPage after all...). Instead you should position the headers and footers using direct content access.

The sample does it like this:

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

ColumnText.showTextAligned allows you to position headers, footers, etc. exactly where you want them on the page, outside the top and bottom margins normally, without causing any new pages to be generated or any other unwanted effects.