iText5中的标题重叠 [英] Header overlap in iText5

查看:583
本文介绍了iText5中的标题重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PdfPTables为我的PDF文档创建了页眉和页脚。我已经分别在我的PdfPageEventHelper的onStartPage和onEndPage事件中指定了页眉和页脚。

I created a header and footer for my PDF document using PdfPTables. I have specified the headers and footers in my PdfPageEventHelper's onStartPage and onEndPage event's respectively.

我面临的问题是在我的文档中添加一个段落。

The issue I am facing is while adding a paragraph to my document.

当我按如下方式创建新的段落时:

When I create a new Paragraph as follows:

Paragraph content = new Paragraph("This is a test text");
try{
  pdfDocument.add(content);
} catch (DocumentException e){
  e.printStackTrace();
}

内容与标题重叠。我需要的是在页眉和页脚之间设置pargraph。有人可以告诉我,为了将pargraph放在页眉和页脚之间而不是在它们之间我需要做什么。

The content overlaps with the header. What I need is to set the pargraph between the header and the footer. Can someone tell me what do I need to do in order to put the pargraph between the header and footer, instead of on them.

谢谢

推荐答案

在底部20设置多于所需的保证金。
,例如一般来说,你继续从底部走势是40。

Set Margin in bottom 20 more than required. e.g. Generally you keep marging from bottom is 40.

document.setMargins(50, 45, 50, 40);

现在,保持60。

writer=PdfWriter.getInstance(document, out);
document.setPageSize(PageSize.A4);
document.setMargins(50, 45, 50, 60);
document.setMarginMirroring(false);

writer.setPageEvent(new HeaderAndFooter());
document.open();

现在在HeaderFooter PageEvent中将footer设置为document.bottom() - 20位。

Now in HeaderFooter PageEvent set Footer at document.bottom() - 20 position.

public class HeaderAndFooter extends PdfPageEventHelper {
    private Font footerFont;
    public HeaderAndFooter() {
        super();
        footerFont = getFontObj(BaseColor.LIGHT_GRAY, 15);
        footerFont.setStyle(Font.ITALIC);
    }


    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.format("Page %d", writer.getPageNumber()),footerFont), (document.left() + document.right())/2 , document.bottom()-20, 0);
    }
}

它将解决重叠问题。它对我来说很好。

It will solve the problem of overlapping. It is working fine for me.

这篇关于iText5中的标题重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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