gpt4 book ai didi

java - iText PDF 正文内容在页 footer 分重叠

转载 作者:行者123 更新时间:2023-11-29 03:16:36 26 4
gpt4 key购买 nike

我用的是itextpdf-5.5.3版本。当我生成 PDF 并使用 PdfPTable 时,它​​重叠在页 footer 分。

enter image description here

PDF 的一些代码。

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

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

对于页脚,

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 canvas = writer.getDirectContent();
canvas.rectangle(50, 30, 500, 780);
canvas.setColorStroke(BaseColor.LIGHT_GRAY);
canvas.stroke();

PdfContentByte cb = writer.getDirectContent();
footerFont.setSize(15);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("____________________________________________________________",footerFont), ((document.left() + document.right())/2)+1f , document.bottom()+20, 0);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.format("Page %d", writer.getPageNumber()),footerFont), (document.left() + document.right())/2 , document.bottom(), 0);

}

}

有什么办法可以避免重叠吗?

最佳答案

我应用了数学逻辑。

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

这里我将底部边距从 40 增加到 60。

然后,在 HeaderFooter 类中,我将边距设置为负号。

ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("____________________________________________________________",footerFont), ((document.left() + document.right())/2)+1f , document.bottom(), 0);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.format("Page %d", writer.getPageNumber()),footerFont), (document.left() + document.right())/2 , document.bottom()-20, 0);

它解决了 PdfPTable 在页脚上重叠的问题。

关于java - iText PDF 正文内容在页 footer 分重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26214007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com