Aspose.PDF Java: Add PNG Image to PDF Document - inserted image is upside down and at wrong coordinates

issue_test_.zip (201.9 KB)
aspose.png (4.5 KB)

Environment:
Aspose.Cell:21.5
Aspose.PDF:21.3

There are three files in the zip file:
2021-05-18 23-03-16屏幕截图.png (8.2 KB)

  1. issue_test_21.5.xlsx is the source test file
  2. issue_test_cell_21.5_asspose.pdf is converted by Aspose.Cell , and then use Aspose.PDF add aspose.png to the pdf. The inserted image is upside down.
  3. issue_test_cell_21.5_aspose.pdf is converted by Microsoft Office, and then use Aspose.PDF add aspose.png to the pdf. The image was inserted correctly.

https://docs.aspose.com/pdf/java/complex-pdf-example/
My test code as below:

@Test
public void tttt() throws IOException {
    // Initialize document object
    Document document = new Document("E:\\tmp\\sign\\issue_test\\aspose.pdf");
    // Add page
    Page page = document.getPages().get_Item(1);

    // -------------------------------------------------------------
    Path _dataDir = Paths.get("E:\\tmp\\sign\\issue_test");
    // Add image
    Path imageFileName = Paths.get(_dataDir.toString(), "aspose.png");
    java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File(imageFileName.toString()));
    // Add image to Images collection of Page Resources
    page.getResources().getImages().add(imageStream);

    log.info("mediaBox: {}", page.getMediaBox());
    log.info("mediaBox llx: {}", page.getMediaBox().getLLX());
    log.info("mediaBox lly: {}", page.getMediaBox().getLLY());
    log.info("mediaBox urx: {}", page.getMediaBox().getURX());
    log.info("mediaBox ury: {}", page.getMediaBox().getURY());
    log.info("cropBox: {}", page.getCropBox());
    log.info("margin left: {}", page.getPageInfo().getMargin().getLeft());
    log.info("margin right: {}", page.getPageInfo().getMargin().getRight());
    log.info("margin top: {}", page.getPageInfo().getMargin().getTop());
    log.info("margin bottom: {}", page.getPageInfo().getMargin().getBottom());

    // Using GSave operator: this operator saves current graphics state
    page.getContents().add(new GSave());
    Rectangle _logoPlaceHolder = new Rectangle(220, 330, 420, 530);

    // Create Matrix object
    Matrix matrix = new Matrix(new double[]{
            _logoPlaceHolder.getURX() - _logoPlaceHolder.getLLX(), 0, 0,
            _logoPlaceHolder.getURY() - _logoPlaceHolder.getLLY(),
            _logoPlaceHolder.getLLX(), _logoPlaceHolder.getLLY()});

    // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
    page.getContents().add(new ConcatenateMatrix(matrix));
    XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
    // Using Do operator: this operator draws image
    page.getContents().add(new Do(ximage.getName()));
    // Using GRestore operator: this operator restores graphics state
    page.getContents().add(new GRestore());

    document.save(Paths.get(_dataDir.toString(), "Complex.pdf").toString());
}

One other thing I noticed with the PDF files generated by Aspose.Cell:

  • LLY/URY starts counting from the top when images are inserted
  • When images ares inserted in PDF files not generated by Aspose.Cell the LLY/LLX starts counting form the bottom.

@hanbd.me

Can you please try to add the image with the following code as explained in Add Image stamps in PDF programmatically with Java.

// Open document
Document pdfDocument = new Document(dataDir + "AsposeCells.pdf");            
// Create image stamp
ImageStamp imageStamp = new ImageStamp(dataDir + "aspose.png");
imageStamp.setXIndent(220);
imageStamp.setYIndent(330);
imageStamp.setHeight(200);
imageStamp.setWidth(200);

// Add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(imageStamp);

// Save output document
pdfDocument.save(dataDir + "AddImageStamp_out.pdf");

Thank you very much.
Your suggestion works. Now I can add image to PDF correctly.
But I’m still confused. Whether the question mentioned before is a bug nor my code (add image using CMT) is wrong?

@hanbd.me

Thanks for your feedback. Your previous code includes several steps where we need to specify coordinates carefully. Whereas, the code for adding image stamp is simple and easy so less chances of confusion. It’s good to know that suggested code has proved to be working on your end.