-2

I want to insert a table over a watermark image. I have followed many steps but all help in writing over the image not inserting a table.

Edit: the image should be inserted in each page in the pdf document. i want to write over it using pdfdTable. all searches describe how to write a text over a watermark.

e.g: document.add(getWatermarkedImage(cb, img, "some text"));

if i added the pdfdtable to the document as document.add(table); it wont be over the image. it will inserted above the image.

as this is my table:

outerCell = new PdfPCell(new Paragraph(" header 1"));
outerCell.setColspan(70);
outerCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
outerTable.addCell(outerCell);

how could i insert this outertable above the watermark

BDeveloper
  • 1,175
  • 6
  • 24
  • 44
  • This question should be closed because it's not clear: are you creating a document from scratch, or are you adding a watermark to an existing document? Are you using iText 5 or are you using iText 7. You claim that you *have followed many steps*, but why would anyone believe you if you don't show us what you've tried. Have you even read the documentation. There are literally a dozen [examples about watermarks](http://developers.itextpdf.com/search/node/watermark) on the official web site. Surely one of these examples can help you? If not, why not? – Bruno Lowagie Jun 25 '16 at 11:23
  • If you are creating a document from scratch, you need to read the answer to [How to add a watermark to a page with an opaque image?](http://developers.itextpdf.com/question/how-add-watermark-page-opaque-image) If you're adding a watermark to an existing document, you need [How to watermark PDFs using text or images?](http://developers.itextpdf.com/question/how-watermark-pdfs-using-text-or-images) I'd close the question by marking it as a duplicate of a [question that explains how to make images transparent](http://stackoverflow.com/questions/27241731/), but that answer never got a vote. – Bruno Lowagie Jun 25 '16 at 11:26
  • 1
    Show us all the many steps you followed and someone (not me) will step in to tell you what you need to change. Without your code, no answer. – Amedee Van Gasse Jun 25 '16 at 12:33
  • Thank you for editing the question and admitting that you didn't read the documentation. Next time, please read the documentation. – Bruno Lowagie Jun 26 '16 at 06:26

1 Answers1

1

Your question sounds wrong. It's as if you want to add an image watermark to a document, which is adequately explained in many other questions such as Set a fix background image for all my pages in PDF iText ASP C# and How can I add an image to all pages of my PDF? (These answers are about C# questions, but the official web site has many watermark and page event examples.)

Just like madhesh, you confuse the StackOverflow readers, even after you've read my answer to the question How to add text to an image? where I wrote:

This is a good example of a question that is phrased in a way that nobody can answer it correctly. After a lot of back and forth comments, it was finally clear that the OP wanted to add a text watermark to an image. Badly phrase questions can be very frustrating. Please take that in consideration when asking a question. In this case, I gave several wrong answers before it became clear what was actually asked.

You want to add a PdfPTable to an Image and your knowledge is insufficient to understand and adapt the examples provided with the answers to questions such as How to add text to an image? and Draw Lines on Image in PDF using ItextSharp.

What you're asking is extremely simple though.

The getWatermarkedImage() method that you mention looks like this:

public Image getWatermarkedImage(PdfContentByte cb, Image img) 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);
    template.saveState();
    template.setColorStroke(BaseColor.GREEN);
    template.setLineWidth(3);
    template.moveTo(width * .25f, height * .25f);
    template.lineTo(width * .75f, height * .75f);
    template.moveTo(width * .25f, height * .75f);
    template.lineTo(width * .25f, height * .25f);
    template.stroke();
    template.setColorStroke(BaseColor.WHITE);
    template.ellipse(0, 0, width, height);
    template.stroke();
    template.restoreState();
    return Image.getInstance(template);
}

This is the example taken from the answer to the question How to draw lines on an image?

You don't want to draw lines, so you can remove all the code that draws lines:

public Image getWatermarkedImage(PdfContentByte cb, Image img) 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);
    // removed the code that draws lines
    return Image.getInstance(template);
}

Instead of lines, you want to draw a PdfPTable, so we add some code where it says // removed the code that draws lines. As documented, adding a table at an absolute position is done with the writeSelectedRows() method:

public Image getWatermarkedImage(PdfContentByte cb, Image img) 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);
    PdfPTable table = new PdfPTable(2);
    table.setTotalWidth(width);
    table.getDefaultCell().setBorderColor(BaseColor.Yellow);
    table.addCell("Test1");
    table.addCell("Test2");
    table.addCell("Test3");
    table.addCell("Test4");
    table.writeSelectedRows(0, -1, 0, height, template);
    return Image.getInstance(template);
}

I adapted one of the many examples in the official documentation (see WatermarkedImages5), and this is the result:

enter image description here

All the functionality you needed to achieve this was available in the documentation. Please read that documentation before you post another question.

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165