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

Free Support Forum - aspose.com

Wrong position and rotation of inserted image in pdf

Hi support team!


I get an issue with the position and rotation of inserted image in pdf.
I try to insert an normal image to pdf with the default coordinate (should display in bottom-left)
but I get a wrong result the image displays on the top-left of page and reversed.
when i try with method “clearContents()” i get the correct one. I don’t know how to fix it now.
pdf file (test1.pdf) for testing was attached

Hi there,


Thanks for your inquiry. I have tested the scenario with sample image using Aspose.Pdf for Java 17.2.0 and unable to notice the reported issue. Please share your sample code along with the image, so we will further investigate the issue and will guide you accordingly.

Document pdfDocument = new Document(“test1.pdf”);<o:p></o:p>

//create image stamp

ImageStamp imageStamp = new ImageStamp("image.png");

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

page.addStamp(imageStamp);

//save output document

pdfDocument.save("ImageStamp_output.pdf");

We are sorry for the inconvenience.

Best Regards,

I just follow the example code in document for java:


public static void insertImage(InputStream stream, String imagePath) throws IOException {

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(stream);
// Set coordinates
int x = 0;
int y = 0;
int w = 300 + 0;
int h = 200 + 0;
Page page = pdfDocument.getPages().get_Item(1);

java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File(imagePath));
page.getResources().getImages().add(imageStream);

page.getContents().add(new Operator.GSave());

Rectangle rectangle = new Rectangle(x, y, w, h);
com.aspose.pdf.Matrix matrix = new com.aspose.pdf.Matrix(new double[]{
rectangle.getWidth() - rectangle.getX(), 0, 0, rectangle.getHeight() - rectangle.getY(), rectangle.getX(), rectangle.getY()});

page.getContents().add(new Operator.ConcatenateMatrix(matrix));

XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
page.getContents().add(new Operator.Do(ximage.getName()));

page.getContents().add(new Operator.GRestore());

pdfDocument.save(“result.pdf”);
}


public static void main(String[] args) throws FileNotFoundException, IOException, Exception {

loadLinesce(“Aspose.Total.Java.lic”);
File f = new File(“test1.pdf”);
InputStream is = new FileInputStream(f);

insertImage(is, “test.png”);

}


and got the result in wrongly

Hi there,


Thanks for sharing addition information. I have tested the scenario with shared code and noticed the reported issue, so logged a ticket PDFJAVA-36556 in our issue tracking system for further investigation and rectification. We will notify you as soon as it is resolved.

However as a workaround, you can add image using ImageStamp object as suggested above.

We are sorry for the inconvenience.

Best Regards,