How to move existing image with Aspose.Pdf

Hi Support,

I want to know what is the best pratice to move an existing image with c# ?
I’m currently using Aspose.pdf with ImagePlacementAbsorber to know where is images on PdfPage.

After that, I’m struggling to move an existing image with aspose.

Have you any idea ?

Thanks in advance.

@Tenjim

If possible can you please share a sample PDF along with an expected output PDF that you want to achieve. We will check the feasibility and share our feedback with you.

cspace invaders.pdf (916.8 KB)

For example, in this pdf, I want to move center image from first page. There is an other image I want to move in the second page, it’s the monitor image. It has been recognized as an image with the imageplacementabsorber.

@Tenjim

Do you want to move the images to some other page or simple want to move it inside same page at different location? Do you have information of location or coordinates at where you want to place it?

I want to move in the same page and after that yes, I want to move other page. Just for your information, I have an control with an adorner to move it and when I stop to moving my goal is to move that image into the pdf.

@Tenjim

We need to investigate this feature in details.

Can you please also clarify against above question?

Yes like I said before, with my adorner I can have the position of my rectangle when I stop moving my adorner. But I don’t have any idea how to proceed to move an existing image from Page.Ressources.Images to the new position. Only way to know the position of an image, is to use ImagePlacementAbsorber and there is an lot of property from Ximage that we can’t modify.

Can you provide me some snippets or way/guidance to move existing image from the same page into a pdf ?

@Tenjim

Please allow us to investigate the feasibility of your requirements and we will get back to you in a while.

1 Like

Hi @asad.ali ,

Do you have any update about this ? It’s really an blocking issue for me.

Kinds Regards,

@Tenjim

We tried something below but we are not sure if it will meet your requirements. We are not sure how your adorner will return the rectangle coordinates and those coordinates will be translated inside PDF via API:

// Load the PDF document
Document pdfDocument = new Document("input.pdf");

// Initialize ImagePlacementAbsorber to find images
ImagePlacementAbsorber absorber = new ImagePlacementAbsorber();
pdfDocument.Pages.Accept(absorber);

// Loop through images found in the PDF
foreach (ImagePlacement imagePlacement in absorber.ImagePlacements)
{
    // Get image's position and dimensions
    Aspose.Pdf.Rectangle rect = imagePlacement.Rectangle;

    // Move the image by modifying the matrix
    imagePlacement.Matrix.Transform(new Aspose.Pdf.Rectangle(0, 0, 100, 100));
}

// Save the updated PDF
pdfDocument.Save("output.pdf");

Therefore, we have logged an investigation ticket as PDFNET-58332 in our issue management system. We will look into details of this feature and let you know once the ticket is resolved. In the meanwhile, can you please share extracted coordinates by your adorner with the screenshot of page with those coordinates? It will help us in investigation.

Hi @asad.ali,

There is somes issues on your snippet :

Aspose.Pdf.Content not existing on this sample.

We cannot use “imagePlacement.Matrix = matrix;” because Matrix from Imageplacement don’t have any setter. Just a getter.

I have tried to use Transform method just like that : imagePlacement.Matrix.Transform(new Point(0, 0)); but nothing moving.

fyi: I’m using Aspose PDF 24.9.0.

Do you have an other way ?

Best Regards.

@Tenjim

We apologize that we shared wrong testing code snippet. We have revised it now in our previous reply.

Nevertheless, can you please reply for the above question?

Hi @asad.ali ,

This code snippet “imagePlacement.Matrix.Transform(new Aspose.Pdf.Rectangle(0, 0, 100, 100));” don’t move any images.
I have put my case testing in attachment with a screenshoot with the red adorner on the image I want to move. The red adorner is what I get from ImagePlacement Absorber.

cspace invaders.pdf (916.8 KB)

Capturespace.JPG (55.2 KB)

Can you do my requirement on your repos sample ? It’s seems that imagePlacement.Matrix.Transform not affect the pdf document.

Can you test your snippet before you send me please?

@Tenjim

The code we shared with you didn’t work in our environment either that is why we logged an issue in our issue tracking system to address this case. We apologize if sharing it caused any kind of confusion for you that it should work. Nevertheless, further details provided by you have been added to the already logged ticket and as soon as we make some progress towards its resolution, we will inform you and showcase the code snippet in our examples as well.