-1

I found that OpenPDF's Image.class seems to have only 6 image positioning methods, and among them, there is only the option to add the image below the text (that is Image.UNDERLYING), but there is no option to let the image float above the text.

The source code of OpenPDF's Image class is as follows:

    public static final int DEFAULT = 0;
    public static final int RIGHT = 2;
    public static final int LEFT = 0;
    public static final int MIDDLE = 1;
    public static final int TEXTWRAP = 4;
    public static final int UNDERLYING = 8;

I want to implement the function of editing the contract. It needs to put a seal image on top of the text, and the position of the image needs to be dynamically set (because the length of the contract text may change). This may involve pagination and absolute positioning. I found that the PdfContentByte class seems to be able to add images to the text, but I don't know how it can dynamically position the images. It would be nice if there was a way to do this in sequence with Document#add like the Paragraph class, but I haven't found it yet.

The effect I want to achieve is roughly like this: Click to view the picture

How can I achieve my needs?

1 Answers1

0

If you want to add something to an existing PDF with OpenPdf, you surely use a PdfStamper.

To add an image above the existing content of some page, simply retrieve the overcontent of the target page from the stamper and add the image to it.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • wow! Great, this is something I didn't understand before, I'm glad you can help me. But I found that the method you provided is also done with the help of `PdfContentByte`. As I said in my post, it is difficult for me to determine which coordinates I want to add the image to after adding the text, so if I use `PdfContentByte`, I don't know how to add the image to the corresponding position exactly (like the picture in my post, I need to add a seal image to some specific Chinese characters). – etherealss Feb 07 '23 at 08:50
  • In order to find a way to locate where I need to add the image, I tried to get the absolute position of some elements, but I didn't find an effective method. – etherealss Feb 07 '23 at 08:51
  • There are different `PdfContentByte` objects in OpenPdf, some are used for adding stuff above, some below existing content. Determining coordinates of text (I assume those characters are text, not graphics) is a completely different question. – mkl Feb 07 '23 at 16:25
  • Thanks for your reply again! What you said is correct, and I also know that `PdfContentByte` can add stuff above the text, after you reply me, I tried to use `PdfStamper` and `PdfContentByte`. But I found that although they can cover the text, it will bring a new problem: that is, I can't precisely locate the position where I need to add the image. So I sadly agree with what you said: This is a completely different question, but it seems that I have to face it if I am going to use `PdfStamper` or `PdfContentByte`. Anyway, you gave me new ideas, thank you very very much! – etherealss Feb 08 '23 at 08:56