Reputation: 195
I am using iText5 with Java for creating pdf and creating document as
document = new Document(new Rectangle(1150f, 1150f));
My content of pdf is getting overridden on the footer( which is an image).
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.
Upvotes: 0
Views: 282
Reputation: 95938
There are multiple issue in your code.
newPage()
during onEndPage()
The event callback onEndPage()
is called during a page change; calling document.newPage()
in that method, therefore, is probably hazardous, at least it is senseless.
document.add
during onEndPage()
As documented for iText and often mentioned in answers and comments on stackoverflow, you shall not use document.add
during onEndPage()
.
You can draw to the direct content (PdfWriter.getDirectContent()
) or the background content (PdfWriter.getDirectContentUnder()
).
You create the Document
using:
document = new Document(new Rectangle(1150f, 1150f));
This constructor applies default margins of 36 units:
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.
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.
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.
Upvotes: 2