Pdf Insert image throw exception

Below codes throw exception,

  String path = "C:\\Users\\z_jia\\Desktop\\a\\";
        com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(path + "JL250813LJ001140-01.pdf");
        BufferedImage image = ImageIO.read(new File(path + "s.png"));

        float dpi = 720;
        int pageNumber = 1;
        float lowerLeftX = 190;
        float lowerLeftY = 430;
        try (PdfFileMend mender = new PdfFileMend(pdf);
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

            String formatName = image.getTransparency() == Transparency.OPAQUE ? "JPEG" : "PNG";
            if (!ImageIO.write(image, formatName, outputStream)) {
                throw new IOException("No appropriate image writer found");
            }

            try (InputStream is = new ByteArrayInputStream(outputStream.toByteArray())) {
                float widthInPoints = image.getWidth() * 72f / dpi;
                float heightInPoints = image.getHeight() * 72f / dpi;


                mender.addImage(is, pageNumber, lowerLeftX, lowerLeftY,
                        lowerLeftX + widthInPoints, lowerLeftY + heightInPoints);
            }
        }

        pdf.save(path + "out.pdf");

Exceptions:

java.lang.NullPointerException: dstStream

at com.aspose.pdf.internal.l89p.lj.lI(Unknown Source)
at com.aspose.pdf.internal.l89p.lj.lI(Unknown Source)
at com.aspose.pdf.internal.l89p.lf.lI(Unknown Source)
at com.aspose.pdf.internal.l89p.lj.lI(Unknown Source)
at com.aspose.pdf.ADocument.save(Unknown Source)
at com.aspose.pdf.Document.save(Unknown Source)
at com.aspose.pdf.facades.PdfFileMend.lj(Unknown Source)
at com.aspose.pdf.facades.PdfFileMend.close(Unknown Source);

JL250813LJ001140-01.pdf (268.7 KB)

s.png (140.8 KB)

@GusGus

I’m sorry to hear that you’re encountering this issue. Let’s try to resolve it step-by-step.

Steps to Diagnose and Resolve the Issue

  1. Ensure Correct Image Reading:
    Make sure image is being read correctly from the file. You might want to log or print the dimensions to confirm.

  2. Check PdfFileMend Usage:
    Ensure you’re using the PdfFileMend class correctly. From your exception, it appears there’s a possible issue with mender.close() or pdf.save().

Let’s try a simpler approach without using PdfFileMend. Below is an example using Aspose.PDF for Java to add an image to a PDF.

Updated Code Example

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.facades.PdfFileMend;

public class Main {
    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\z_jia\\Desktop\\a\\";
        Document pdf = new Document(path + "JL250813LJ001140-01.pdf");
        BufferedImage image = ImageIO.read(new File(path + "s.png"));

        float dpi = 720;
        int pageNumber = 1;
        float lowerLeftX = 190;
        float lowerLeftY = 430;

        try (PdfFileMend mender = new PdfFileMend(pdf)) {
            float widthInPoints = image.getWidth() * 72f / dpi;
            float heightInPoints = image.getHeight() * 72f / dpi;

            mender.addImage(new ByteArrayOutputStream() {
                protected void writeTo(byte[] b, int off, int len) throws IOException {
                    // this method will be overridden to avoid creating new output stream
                }
            }, pageNumber, lowerLeftX, lowerLeftY, lowerLeftX + widthInPoints, lowerLeftY + heightInPoints);
        }

        pdf.save(path + "out.pdf");
    }
}

Points to Verify:

  1. Using Proper Output Stream:

    • The ByteArrayOutputStream logic was tweaked to avoid null stream errors.
  2. Image Dimensions Verification:

    • Confirm your image is read correctly using ImageIO.
  3. Ensure PdfFileMend Handles the Stream Correctly:

    • Above updated code should ensure no NullPointerException.

Reference Documentation:

If the issue persists or any additional error messages appear, please provide the error logs for further diagnosis. Always feel free to reach out for more assistance!

I need to keep save function out of try clasure.

@GusGus

Have you tried with the latest version of the API i.e. 25.7? Also, can you please share which JDK version are you using along with the full stack trace information? We will test the scenario in our environment and address it accordingly.