Free Support Forum - aspose.com

Blank image is being created while converting PDF to image (Jave API) (Version: 19.3, 20.4)

Dear Aspose Support Team,

Blank image is being created for page number 1, 2 and 4 of attached pdf while converting it to image using following code snippet for both version 19.3 and 20.4:

  public static void getImageFromPdf(String soucePath, String destinationPath, int totalPdfPages)
      throws Exception {
    System.out.println("Start: getImageFromPdf()");
    InputStream pdfFileInputStream = new FileInputStream(soucePath);
    com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(pdfFileInputStream);
    FileOutputStream outStream = null;
    int startPage = 1;
    String[] imageFormats = {"JPG", "JPEG", "TIFF", "TIF", "BMP"};
    for (String imageFormat : imageFormats) {
      File file;
      for (int i = startPage; i <= totalPdfPages; i++) {
        file = new File(destinationPath + i + "-" + imageFormat + "." + imageFormat);
        outStream = new FileOutputStream(file);
        byte imagesBytes[] = convertToImage(pdfDocument, imageFormat, "", i, -1, 300);
        outStream.write(imagesBytes);
        outStream.flush();
        outStream.close();
      }
    }
    pdfFileInputStream.close();
    System.out.println("End: getImageFromPdf()");
  }

  public static byte[] convertToImage(com.aspose.pdf.Document pdfDocument, String imageFormat,
      String password, int pageNum, int imageType, int resolution) throws Exception {
    System.out.println("Start: convertToImage()");
    byte[] imageBytes = null;
    Resolution resolutionObj = new Resolution(resolution);
    ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
    try {
      switch (imageFormat.toUpperCase()) {
        case "JPEG":
        case "JPG":
          JpegDevice jpegDevice = new JpegDevice(resolutionObj, 50);
          jpegDevice.process(pdfDocument.getPages().get_Item(pageNum), imageStream);
          System.out.println("Returning Byte Array Size: " + imageStream.toByteArray().length);
          imageBytes = imageStream.toByteArray();
          break;
        case "TIF":
        case "TIFF":
          TiffSettings tiffSettings;
          TiffDevice tiffDevice;
          if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
            tiffSettings = new TiffSettings();
            tiffSettings.setDepth(ColorDepth.Format1bpp);
            // CompressionType.CCITT4 i.e Black & White
            // CompressionType.LZW i.e GrayScale
            tiffSettings.setCompression(CompressionType.LZW);
            tiffDevice = new TiffDevice(resolutionObj, tiffSettings);
          } else {
            tiffDevice = new TiffDevice(resolutionObj);
          }
          tiffDevice.process(pdfDocument, pageNum, pageNum, imageStream);
          System.out.println("Returning Byte Array Size: " + imageStream.toByteArray().length);
          imageBytes = imageStream.toByteArray();
          break;
        case "BMP":
          BmpDevice bmpDevice = new BmpDevice(resolutionObj);
          bmpDevice.process(pdfDocument.getPages().get_Item(pageNum), imageStream);
          System.out.println("Returning Byte Array Size: " + imageStream.toByteArray().length);
          imageBytes = imageStream.toByteArray();
          break;
        default:
          throw new Exception("Image format not supported");
      }

    } finally {
      // closePdf(pdfDocument);
    }
    System.out.println("End: convertToImage()");
    return imageBytes;
  }

ApplicationForm_20200618_053434986.pdf (324.6 KB)
ConvertedImages.zip (3.1 MB)

Could you please suggest a solution?
Converted images have been attached in Zip file.

Thanks & Regards,
Madhurendra Narayan Tiwary

@edleads

We have reproduced the issue in our environment while testing the scenario with Aspose.PDF for Java 20.6 and logged it as PDFJAVA-39559 in our issue management system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

@asad.ali

Do we have an estimated time when this can be expected to be fixed?
This is affecting production activities.

Regards,
Madhurendra Narayan Tiwary

@edleads

We regret to share that we cannot provide any ETA at the moment as ticket will be resolved on first come first serve basis. The resolution time of the ticket may vary depending upon the number of issues logged prior to it. However, we will keep you informed about the updates as soon as we have some regarding ticket resolution. Please give us some time.

We are sorry for the inconvenience.