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

Free Support Forum - aspose.com

While Inserting Image on existing Pdf, image rotates issue

Hi Team ,

We are using java sdk of aspose. We are inserting image into existing pdf at particular coordinates. But after inserting image , the image get rotates. It is not happening with every pdf.

Below is code sample
Document pdfDocument = new com.aspose.pdf.Document("<<<>>>>>>>>");
String pathOfImage = “image1.jpg”
double xAxisCustom = 248;
double pdfCoord = 30;
Page pdfPage = pdfDocument.getPages().get_Item(1);

FileInputStream imageStream = new FileInputStream(pathOfImage);
pdfPage.getResources().getImages().add(imageStream);
pdfPage.getContents().add(new GSave());
Rectangle rectangle = new Rectangle(xAxisCustom as double, pdfCoord, xAxisCustom as double, pdfCoord);
Double[] db = [100, 0, 0, 40, xAxisCustom as double, pdfCoord] as Double[]
Matrix matrix = new Matrix(db);
pdfPage.getContents().add(new ConcatenateMatrix(matrix));
XImage ximage = pdfPage.getResources().getImages().get_Item(pdfPage.getResources().getImages().size());
pdfPage.getContents().add(new Do(ximage.getName()));
pdfPage.getContents().add(new GRestore());
imageStream.close();

I am also attaching pdfs and images

original_issue.pdf -> it is original pdf that has issue
image1.png ->using this image on original_issue.pdf
original_issue_insert_image.pdf ->updated pdf with rotated image

original.pdf -> it is the pdf which has no issue
image2.png ->using this image on original.pdf
original_insert_image.pdf -> update pdf after inserting image on pdf, it has no issue

Please help me on this topic

image1.png (1.6 KB)
image2.png (1.6 KB)
original.pdf (49.4 KB)
original_insert_image.pdf (51.4 KB)
original_issue.pdf (110.7 KB)
original_issue_insert_image.pdf (108.3 KB)

@deepakblusynergy

We were able to notice the similar issue with Aspose.PDF for Java 20.4 in our environment and have logged it as PDFJAVA-39357 in our issue management system for sake of correction. We will surely look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi Team any update on this. We need to this functionality fix . Can you please update here ?

@deepakblusynergy

We have investigated the earlier logged ticket and as per our findings, the page in original_issue.pdf is rotated on 90 degrees. You could check it by the method pdfPage.getRotate() and could add some rotation matrix for this case.

pdfPage.getContents().add(new ConcatenateMatrix(matrix));

if (pdfPage.getRotate() == Rotation.on90) {
    pdfPage.getContents().add(new ConcatenateMatrix(new Matrix(new double[] {0, 3, -0.5 , 0, 3, 8})));            
}

Also, you should notice other rotation in your calculations (Rotation.on180 and Rotation.on270))).

But instead of all this calculation we recommend using ImageStamp that does not depend on rotations and would be perfect for your case. Please see a code snippet below:

Document pdfDocument = new com.aspose.pdf.Document(dataDir + "original_issue.pdf");
String pathOfImage = dataDir + "image1.png";
Page pdfPage = pdfDocument.getPages().get_Item(1);
ImageStamp stamp = new ImageStamp(pathOfImage);
stamp.setHeight(40);
stamp.setWidth(100);                        
stamp.setHorizontalAlignment(HorizontalAlignment.Center);
stamp.setVerticalAlignment(VerticalAlignment.Bottom);
pdfPage.addStamp(stamp);
pdfDocument.save(dataDir + "output20.5.pdf"); 

I will try your approach and let you know If I face any issue

@deepakblusynergy

Sure.