We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace placeholder with any image in PDF file

@tahir.manzoor
I have a .pdf file and i want to replace the placeholder with some image.
As example,
In my input file placeholder name is “SB_SIGNATURE_INTERNAL_1” i want to replace this with any image.
PFA
Test.zip (76.1 KB)

@rabinintig

You may please try following code snippet in order to replace text with image:

var pdfDocument = new Aspose.Pdf.Document(dataDir + "Test.pdf");

TextFragmentAbsorber absorber = new TextFragmentAbsorber("SB_SIGNATURE_INTERNAL_1");
pdfDocument.Pages.Accept(absorber);

foreach(TextFragment fragment in absorber.TextFragments)
{
 Rectangle pageRect = fragment.Page.Rect;
 Rectangle fragmentRect = fragment.Rectangle;
 MarginInfo marginInfo = fragment.Page.PageInfo.Margin;

 FloatingBox box = new FloatingBox();
 box.Left = fragmentRect.LLX - marginInfo.Left;
 box.Top = pageRect.Height - fragmentRect.URY - marginInfo.Top;
 box.Width = fragment.Rectangle.Width;

 Image img = new Image();
 img.File = dataDir + "aspose.jpg";
 img.FixHeight = fragment.Rectangle.Height;
 img.FixWidth = fragment.Rectangle.Width;
 box.Paragraphs.Add(img);
 //box.Border = new BorderInfo(BorderSide.All, 1);
 fragment.Text = String.Empty;
 fragment.Page.Paragraphs.Add(box);
}

pdfDocument.Save(dataDir + "output.pdf");

OUTPUT.pdf (86.5 KB)

thanks @tahir.manzoor
can you please give this solution on JAVA.

@rabinintig

Please check following Java code for the same purpose:

Document pdfDocument = new Document(dataDir + "Test.pdf");

        TextFragmentAbsorber absorber = new TextFragmentAbsorber("SB_SIGNATURE_INTERNAL_1");
        pdfDocument.getPages().accept(absorber);

        for(TextFragment fragment : absorber.getTextFragments())
        {
            Rectangle pageRect = fragment.getPage().getRect();
            Rectangle fragmentRect = fragment.getRectangle();
            MarginInfo marginInfo = fragment.getPage().getPageInfo().getMargin();

            FloatingBox box = new FloatingBox();
            box.setLeft(fragmentRect.getLLX() - marginInfo.getLeft());
            box.setTop(pageRect.getHeight() - fragmentRect.getURY() - marginInfo.getTop());
            box.setWidth(fragment.getRectangle().getWidth());

            Image img = new Image();
            img.setFile(dataDir + "aspose.jpg");
            img.setFixHeight(fragment.getRectangle().getHeight());
            img.setFixWidth(fragment.getRectangle().getWidth());
            box.getParagraphs().add(img);
            //box.Border = new BorderInfo(BorderSide.All, 1);
            fragment.setText("");
            fragment.getPage().getParagraphs().add(box);
        }

        pdfDocument.save(dataDir + "output.pdf");

Thanks @asad.ali

The above code is working fine as expected.
Can you please help me with another thing.
If I want to find the coordinate of the search string.
As example,
My search string is “SB_SIGNATURE_INTERNAL_1” and my input file is “Test.pdf”.
where the search string will match my input file I need the exact position of my input file where it matches as a form of
a. page number (which page it matches).
b. x-axis and y-axis (the exact location in my input document)

code output will be -->
a. page number (which page it matches).
b. x-axis and y-axis (the exact location in my input document)

please help me on this.

@rabinintig

You can get TextFragment.Page and TextFragment.Position Properties in order to achieve your requirements. For example, please add following lines for found text:

for(TextFragment fragment : absorber.getTextFragments())
{
 Page page = fragment.getPage();
 int pagenumber = fragment.getPage().getNumber();
 Position position = fragment.getPosition();
 double X = position.getXIndent();
 double Y = position.getYIndent();
}

thanks @asad.ali
Please help me with one more thing.
As of now, I am able to search any string with help of “NewTest.java” code
and I am getting page number, x-axis, Y-axis.
output is -
pagenumber = 1
X-axis = 153.35
Y-axis = 544.042

Now this same output will be the input of other code which will help me based on the page number, X-axis,Y-axis it will replace the image with my own size what I will define.
In my new code the input will be
inputFile = “i attached the pdf file”
pagenumber = 1
X-axis = 153.35
Y-axis = 544.042
image = “you can use any image”

Output will be -
one pdf file with replace of image
PFA
NewTest.zip (76.8 KB)

@rabinintig

You can use image stamp to insert image at given page number and X,Y coordinates:

Page page = fragment.getPage();
int pagenumber = fragment.getPage().getNumber();
Position position = fragment.getPosition();
double X = position.getXIndent();
double Y = position.getYIndent();
// this is how above values will be used
ImageStamp stamp = new ImageStamp(dataDir + "aspose.jpg");
stamp.setWidth(fragment.getRectangle().getWidth());
stamp.setHeight(fragment.getRectangle().getHeight());
stamp.setXIndent(X);
stamp.setYIndent(Y);
stamp.setBackground(false);
pdfDocument.getPages().get_Item(pagenumber).addStamp(stamp);

Hi @asad.ali

In this case i don’t have search string as like “SB_SIGNATURE_INTERNAL_1” .So, how i will create the “fragment” for this (Page page = fragment.getPage()) code.
In this case have to add the image based on pagenumber, X-axis,Y-axis only.
My code will like,

  1. first based on pagenumber i will go to that page.
  2. then i will add the image based on X-axis, Y-axis.
  3. the Y-axis value is my pixels down from the top left corner.
    I attached my code it takes “stamp.setYIndent(600)” Yindex as down to top but i need my Yindex will go as down from the top left corner..
    Please help me on this.
    PFA
    NewTest.zip (759 Bytes)

@rabinintig

As you are already passing page number to your separate method for image placement, you can get page as following:

com.aspose.pdf.Page page = pdfDocument.getPages().get_Item(pagenumber);

Please note that PDF format follows a coordinating system where origin starts from lower-left corner which means (0,0) is lower-left. We already have shared a code snippet that returns the correct position of text which was searched and an image stamp can be placed correctly using those values.

Would you please explain a bit more about the values in pixels from where and how you are getting them. We will further investigate the scenario and address it accordingly.

@asad.ali

i am geeing the input from external system.
my input is like.

  1. one .pdf file
  2. one image ( you can use any image)
  3. x-axis and y-axis
  4. page number
    I have to find the page number in the pdf file then move to X and Y axis then add the image.
    I an attached my input pdf and input JSON and in “input.png” I highlighted for which position the external system wants to add the image. But when i used your code that time image placed in a different place.
    PFA
    input.zip (89.8 KB)

@rabinintig

Please try using following code snippet:

Document pdfDocument = new Document(dataDir + "hi rabin.pdf");
Page page = pdfDocument.getPages().get_Item(3);
ImageStamp stamp = new ImageStamp(dataDir + "aspose.jpg");
stamp.setWidth(50);
stamp.setHeight(30);
stamp.setXIndent(78);
stamp.setYIndent(page.getPageInfo().getHeight() + page.getPageInfo().getMargin().getTop() - 333);
stamp.setBackground(false);
page.addStamp(stamp);
pdfDocument.save(dataDir + "output.pdf");

output.pdf (103.5 KB)

@asad.ali
Above code is working for only the same input document.but when i use other document with the other X and Y axis that time it’s not working.I attached the input file and the position also.
I need a generic code for this.
PFA
Test.zip (36.6 KB)

@rabinintig

We have tested the scenario in our environment and observed the issue. However, we also noticed that X and Y axis values shared by you are not consistent in terms of pixels or points. The code which we shared in our previous reply, can be used generically if values are consistent. Previous code was prepared considering the values were according to top-left as starting point. So in the case of your new PDF document and stamp position, value of Y-axis should be 560. output560.pdf (50.5 KB)

While using your current Y-axis values (considering both top-left and bottom-left as starting point), the image stamp is not being added correctly. We will surely proceed further to investigate and address your issue. However, could you please share that from which source you are getting X and Y values and how the measurement is performed for them.

@asad.ali
I have my own application which is sending this X and Y axis.Any it’s change every time.
Please help me on this.

@rabinintig

We will surely investigate and try to share a suitable working code snippet with you. However, would you please share some details about how you are extracting or defining the X and Y axis. Please share some sample console application which can show us the extraction of coordinates and we can then try to prepare a method to process these coordinates accordingly.