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);
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
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.
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:
Using Proper Output Stream:
The ByteArrayOutputStream logic was tweaked to avoid null stream errors.
Image Dimensions Verification:
Confirm your image is read correctly using ImageIO.
Ensure PdfFileMend Handles the Stream Correctly:
Above updated code should ensure no NullPointerException.
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!
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.