-2

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:

  1. Add some text on the last page through Pdfcontentbyte.
rahlrokks
  • 451
  • 1
  • 4
  • 12
  • Please read the documentation before posting a question. For instance: there's a free ebook entitled [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) and it contains the answer to this question: [How to add text to an image?](http://stackoverflow.com/questions/26814958/how-to-add-text-to-an-image) That's exactly what you're asking here: not to add text on the last page, but to add text on an image. – Bruno Lowagie Feb 14 '15 at 12:14
  • Hi Bruno, But my Image is on the last page and to add text on that image I need the reference of last page. – rahlrokks Feb 14 '15 at 12:24
  • Please read my answer to the duplicate question instead of trying to argue with me. In the first approach, I create a `PdfTemplate` to which I add the image and the text. Then I add that `PdfTemplate`. **It doesn't matter where I add that template, does it?** In the second approach, I add the text to the `PdfContentByte` through a cell event. If that cell happens to be on the last page, the content is added on the last page. This seems like **exactly** what you need. – Bruno Lowagie Feb 14 '15 at 12:30
  • It works for me and http://lowagie.com/doesntwork – Bruno Lowagie Feb 14 '15 at 12:41
  • I checked the pdf you created in the answer. That's what I'm exactly trying to do. But your method is creating a different image on first page not editing the last one. document.Add(getWatermarkedImage(cb, iTextSharp.text.Image.GetInstance(JpgSign), "Testing")); – rahlrokks Feb 14 '15 at 12:44
  • And why does that matter? An image is an image is an image. You can add it on any page you like! Why are you playing the wiseguy? – Bruno Lowagie Feb 14 '15 at 12:47

1 Answers1

1

The question posted by rahlrokks was originally an exact duplicate of How to add text to an image? but:

  1. rahlrokks didn't understand that answer.
  2. rahlrokks changed his question in a rather strange way.

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.

enter image description here

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:

enter image description here

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.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165