Example :
We create a blank PDF, and add an image ("Image1.jpg")using PDFBitmap.fromFile() => it works ("Image Jpg 1.pdf")
If we get a buffered image of the same image and add it using new PDFBitmap(BufferedImage) => this fail when copying to the canvas.
We saved the jpeg as "Image Jpg 2.jpg" to check it was correctly loaded
- Code: Select all
public void loadJpeg() {
String inputFile = "Image 1.jpg";
float width = 400f;
float height = 300f;
float x = 180f;
float y = 70f;
// Test
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.appendPage();
PdfImage pdfImage1 = PdfImage.fromFile(inputFile);
page.getCanvas().drawImage(pdfImage1, x, y, width, height);
// Save the result document
pdf.saveToFile("image Jpg 1.pdf");
try {
pdf = new PdfDocument();
page = pdf.appendPage();
File f = new File(inputFile);
InputStream is = new BufferedInputStream(new FileInputStream(f));
BufferedImage bf = Imaging.getBufferedImage(is);
// to check if the image was correctly loaded
ImageIO.write(bf, "jpg", new File("Image 2.jpg"));
PdfImage pdfImage2 = new PdfBitmap(bf);
page.getCanvas().drawImage(pdfImage2, x, y, width, height);
// Save the result document
pdf.saveToFile("image Jpg 2.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
The error we get at runtime is the following :
java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 1920000 out of bounds for byte[1440000]
The error ocurs when calling the DrawImage from canvas
I'm using Spire Office for Java, v11.2.0, on windows