I'm creating a PDF document using iTextSharp and it contains a table that spans multiple pages. After adding the table, my PDF document has 3 pages.
Now what I want to do is:
- Add some text on the last page through Pdfcontentbyte.
I'm creating a PDF document using iTextSharp and it contains a table that spans multiple pages. After adding the table, my PDF document has 3 pages.
Now what I want to do is:
The question posted by rahlrokks was originally an exact duplicate of How to add text to an image? but:
This forced me to create the WatermarkedImages3 example.
It uses the method mentioned in my answer to How to add text to an image?
public Image getWatermarkedImage(PdfContentByte cb, Image img, String watermark) throws DocumentException {
float width = img.getScaledWidth();
float height = img.getScaledHeight();
PdfTemplate template = cb.createTemplate(width, height);
template.addImage(img, width, 0, 0, height, 0, 0);
ColumnText.showTextAligned(template, Element.ALIGN_CENTER,
new Phrase(watermark, FONT), width / 2, height / 2, 30);
return Image.getInstance(template);
}
Rahlrokks claims that this doesn't work. It is easy to prove that rahlrokks is not telling us the truth. If we look at watermark3.pdf, we clearly see that we're on the last page (2 of 2) and we see the text added on top of the image.
If you look at my code, you can even see that I even answered his updated question:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(1);
for (int i = 0; i < 50; i++) {
table.addCell("rahlrokks doesn't listen to what people tell him");
}
PdfContentByte cb = writer.getDirectContentUnder();
table.addCell(getWatermarkedImage(cb, Image.getInstance(IMAGE1), "Bruno"));
document.add(table);
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("Bruno knows best"), 260, 400, 45);
document.close();
}
Not only am I adding text on the image, I am also adding text on the last page using PdfContentByte
:
I'm sorry rahlrokks, but you should not say it doesn't work in cases where it's so easy to prove that it does work.