繁体   English   中英

iText5 PDF内容在页脚上被覆盖

[英]iText5 PDF content getting overridden on Footer

我正在将iText5与Java一起用于创建pdf和创建文档

document = new Document(new Rectangle(1150f, 1150f));

我的pdf内容在页脚(图像)上被覆盖。

页脚代码:

public void onEndPage(PdfWriter writer, Document document) {    
        document.newPage();
        try {           
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("Page %d", writer.getPageNumber())), (document.left() + document.right())/2,document.bottom()-18,0);            
            Image image = Image.getInstance(PdfHeaderFooter.class.getResource("/static/images/SampleFooter.png"));
            image.scaleAbsolute(1100f, 75f);// image width,height
            image.setAbsolutePosition(30, 40);
            document.add(image);                           
        }
        catch(DocumentException de) {
            throw new ExceptionConverter(de);
        } catch (MalformedURLException e) { 
            logger.error(ExceptionUtils.getStackTrace(e));  
        } catch (IOException e) {
            logger.error(ExceptionUtils.getStackTrace(e));  
        }
    }

另外,一些搜索建议使用边距解决方案 设置边距,但我找不到设置边距或其他任何解决方案的确切位置。

请帮助,当内容超出pdf区域且页脚图像上没有重叠时,我应该如何创建新页面。

您的代码中存在多个问题。

newPage()期间的onEndPage()

页面更改期间将调用事件回调onEndPage() 因此,在该方法中调用document.newPage()可能很危险,至少没有意义。

document.add期间onEndPage()

正如有关iText的文档所述,并且经常在关于stackoverflow的答案和注释中提到, 您不应在onEndPage()期间使用document.add

您可以绘制直接内容( PdfWriter.getDirectContent() )或背景内容( PdfWriter.getDirectContentUnder() )。

座标

您使用以下方法创建Document

document = new Document(new Rectangle(1150f, 1150f));

此构造函数应用36个单位的默认边距:

public Document(Rectangle pageSize) {
    this(pageSize, 36, 36, 36, 36);
}

因此,您的内容将被写入36 <x <1114和36 <y <1114的矩形中。

现在,您像这样添加图像

image.scaleAbsolute(1100f, 75f);// image width,height
image.setAbsolutePosition(30, 40);

该图像位置是图像的左下角 因此,您打算在30 <x <1130和40 <y <115的矩形中绘制图像。

因此,图像显然会与内容的一部分重叠。 向下移动图像或使用底边距足够大的显式边距。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM