Free Support Forum - aspose.com

Add Image error com.aspose.pdf.internal.ms.System.lh: Invalid image stream (stream)

I am trying to insert images as pdf pages within an already created pdf document at various page numbers but I am getting the following exception com.aspose.pdf.internal.ms.System.lh: Invalid image stream (stream) com.aspose.pdf.XImageCollection.lf(Unknown Source) com.aspose.pdf.XImageCollection.lI(Unknown Source) com.aspose.pdf.XImageCollection.lI(Unknown Source) com.aspose.pdf.XImageCollection.lI(Unknown Source) com.aspose.pdf.XImageCollection.lI(Unknown Source) com.aspose.pdf.XImageCollection.lI(Unknown Source) com.aspose.pdf.XImageCollection.lf(Unknown Source) com.aspose.pdf.XImageCollection.add(Unknown Source)

Below is the code I am using to append images as pages into the pdf:
InputStream is = new FileInputStream();
Document pdfDocument = new Document(is);
PageCollection pageList = pdfDocument.getPages();
ByteArrayInputStream imageStream = new ByteArrayInputStream(PdfUtils.generateImage());
for(int i = 0 ; i < cmds.getNumberOfPages() ; i++) {
Page newPage = pageList.add();
newPage.getResources().getImages().add(imageStream);
}
Where:

  1. PdfUtils.generateImage() produces a byte array

Code for generating image to insert as a page:
public static byte[] generateImage() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(200000);
int width = 595;
int height = 842;
int[] data = new int[width * height];
int i = 0;
for (int y = 0; y < height; y++) {
int red = (y * 255) / (height - 1);
for (int x = 0; x < width; x++) {
int green = (x * 255) / (width - 1);
int blue = 128;
data[i++] = (red << 16) | (green << 8) | blue;
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, data, 0, width);
ImageIO.write(image, “JPEG”, out);
return out.toByteArray();
}

Attaching the sample PDF in the post: 2.pdf (35.9 KB)

Test cases:

  1. When cmds.getNumberOfPages() = 2, result = Exception
  2. When cmds.getNumberOfPages() = 1, result = Insertion is successful

@teenthofabud

Would you kindly share your sample PDF document along with sample images you are trying to insert into it. We will test the scenario in our environment and address it accordingly.

I have updated my question with sample pdf, sample image and failing test cases

@teenthofabud

Thank you for sharing requested data.

We have been able to reproduce the issue in our environment. A ticket with ID PDFJAVA-39032 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

The issue shows being resolved. How can I get the fix?

@teenthofabud

Thank you for getting back to us.

Please note that a real adding of image happens in process of saving, so the initialization of imageStream should be done inside the loop:

Document pdfDocument = new Document(is);
PageCollection pageList = pdfDocument.getPages();
for(int i = 0 ; i < cmds.getNumberOfPages() ; i++) {
   Page newPage = pageList.add();
   ByteArrayInputStream imageStream = new ByteArrayInputStream(generateImage());  
   //newPage.getResources().getImages().add(imageStream);
   newPage.addImage(imageStream, newPage.getRect());
}

Furthermore, we recommend using the code:

newPage.addImage(imageStream, newPage.getRect());

instead of:

newPage.getResources().getImages().add(imageStream);

We hope this will be helpful. Please feel free to contact us if you need any further assistance.