简体   繁体   English

iText5 PDF内容在页脚上被覆盖

[英]iText5 PDF content getting overridden on Footer

I am using iText5 with Java for creating pdf and creating document as 我正在将iText5与Java一起用于创建pdf和创建文档

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

My content of pdf is getting overridden on the footer( which is an image). 我的pdf内容在页脚(图像)上被覆盖。

Footer code: 页脚代码:

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

Also, some searches suggested margin solution . 另外,一些搜索建议使用边距解决方案 to set margin but I am not able to find the exact place to set the margin or any other solution. 设置边距,但我找不到设置边距或其他任何解决方案的确切位置。

Please help, how should I create a new page when content goes out of the pdf area and is not overlapped on footer image. 请帮助,当内容超出pdf区域且页脚图像上没有重叠时,我应该如何创建新页面。

There are multiple issue in your code. 您的代码中存在多个问题。

newPage() during onEndPage() newPage()期间的onEndPage()

The event callback onEndPage() is called during a page change; 页面更改期间将调用事件回调onEndPage() calling document.newPage() in that method, therefore, is probably hazardous, at least it is senseless. 因此,在该方法中调用document.newPage()可能很危险,至少没有意义。

document.add during onEndPage() document.add期间onEndPage()

As documented for iText and often mentioned in answers and comments on stackoverflow, you shall not use document.add during onEndPage() . 正如有关iText的文档所述,并且经常在关于stackoverflow的答案和注释中提到, 您不应在onEndPage()期间使用document.add

You can draw to the direct content ( PdfWriter.getDirectContent() ) or the background content ( PdfWriter.getDirectContentUnder() ). 您可以绘制直接内容( PdfWriter.getDirectContent() )或背景内容( PdfWriter.getDirectContentUnder() )。

coordinates 座标

You create the Document using: 您使用以下方法创建Document

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

This constructor applies default margins of 36 units: 此构造函数应用36个单位的默认边距:

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

Thus, your content will be written into the rectangle with 36 < x < 1114 and 36 < y < 1114. 因此,您的内容将被写入36 <x <1114和36 <y <1114的矩形中。

Now you add your image like this 现在,您像这样添加图像

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

The image position is the lower left corner of the image. 该图像位置是图像的左下角 Thus, you intend to draw the image in the rectangle with 30 < x < 1130 and 40 < y < 115. 因此,您打算在30 <x <1130和40 <y <115的矩形中绘制图像。

Thus, the image obviously will overlap part of the content. 因此,图像显然会与内容的一部分重叠。 Either move your image down or use explicit margins with a large enough bottom margin. 向下移动图像或使用底边距足够大的显式边距。

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

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