Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Mar 03, 2026 1:31 pm

We have a program that needs to add some image file to a PDF after changing them, but but this fail.

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

GuiPATRY
 
Posts: 37
Joined: Wed Feb 17, 2021 10:18 am

Tue Mar 03, 2026 1:52 pm

Additional Information : when the using the same file converted to different format, the result is :
PNG : same error
TIFF : same error
BMP : OK (not present in ZIP file as too big)

GuiPATRY
 
Posts: 37
Joined: Wed Feb 17, 2021 10:18 am

Wed Mar 04, 2026 2:16 am

Hello,

Thanks for your inquiry.
Since the "Imaging" class in your code is not provided, we are unable to further analyze your issue. However, using the following code to convert an image to PdfBitmap and add it to a page, the program works correctly. Please try using this method. If this does not meet your needs, please reply to us with more details. Thank you in advance.
Code: Select all
BufferedImage image1 = ImageIO.read(new File("Image 1.jpg"));
PdfImage pdfBitmap = new PdfBitmap(image1);

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 764
Joined: Mon Dec 27, 2021 2:23 am

Wed Mar 04, 2026 9:10 am

Imaging is an Apache class from commons https://commons.apache.org/proper/commons-imaging/.
I confirm that using the ImageIO class is working fine as a replacement.

Thanks

GuiPATRY
 
Posts: 37
Joined: Wed Feb 17, 2021 10:18 am

Thu Mar 05, 2026 1:32 am

Hello,

Thanks for your feedback. If you encounter any issues with our products in the future, please feel free to write to us at any time.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 764
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF

cron