iTextSharp的添加页眉和页脚,而不是外部的页边距页边距内显示。 [英] itextsharp adding header and footer, displayed inside the page margin not outside the page margin.

查看:1058
本文介绍了iTextSharp的添加页眉和页脚,而不是外部的页边距页边距内显示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来iTextSharp的,只是不知道如何放置页眉和页脚边距范围外的页面活动结束后?似乎当我用的,而不是添加页脚页边距之外的OnEndPage事件,它增加了页边距内,它总是产生计算器异常时,它会在底部边缘,它应该补充保证金之外吗?



是否有任何设置来告诉添加在文档页脚文本保证金之外(或者是填充)?



< p在提前>感谢。


解决方案

由于你是新来的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 stackoverflow 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.

这篇关于iTextSharp的添加页眉和页脚,而不是外部的页边距页边距内显示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆