Convert a document to image format using Java

Hi,

I am getting a runtime error using the new 3.0 java library

this is the sample code i am using:

public void convertToBase64File(String fileName, ConversionConfig config, int nLoop) {

InputStream inputStream = null;
Base64InputStream b64is = null;
try {
calculateMemoryStats(“2”);
ConversionHandler conversionHandler = new ConversionHandler(config);
ImageSaveOptions imageSaveOptions = new ImageSaveOptions();
imageSaveOptions.setConvertFileType(ImageSaveOptions.ImageFileType.Png);

for (int j = 1; j < 6; j++) {
// imageFileName is the finally generated file name
//String imageFileName = conversionHandler.convertToImage(
// fileName, imageSaveOptions);
imageSaveOptions.setPageNumber(j);
try
{
GroupDocsInputStream gis = conversionHandler.convert(fileName, imageSaveOptions);
b64is = null;
b64is = new Base64InputStream(gis.toInputStream(),true,0,new byte[0]);
FileOutputStream fos = new FileOutputStream(new File(“C://temp//sample_” + nLoop + “_” + j + “.base64”),true);
fos.write((new String("{“source”:“ETMF”,").getBytes()));
fos.write((new String("“cur_pg”:“1”,").getBytes()));
fos.write((new String("“obj_id”:“090001803635252”,").getBytes()));
fos.write((new String("“tot_pg”:“5”,").getBytes()));
fos.write((new String("“rec_id”:“0900018073635”,").getBytes()));
fos.write((new String("“image”:"").getBytes()));
int read = 0;
byte[] bytes = new byte[1024];

while ((read = b64is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
fos.write((new String(""}").getBytes()));
fos.flush();
fos.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (inputStream != null) try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if (b64is != null) try {
b64is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
if (inputStream != null) try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if (b64is != null) try {
b64is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


This is the runtime error:
java.lang.ClassCastException: com.aspose.ms.System.a.a.n cannot be cast to com.groupdocs.foundation.utils.wrapper.stream.GroupDocsInputStream
at com.groupdocs.conversion.examples.GroupDocsUtil.convertToBase64File(GroupDocsUtil.java:219)
at com.groupdocs.conversion.examples.TestMain2.main(TestMain2.java:51)
java.lang.ClassCastException: com.aspose.ms.System.a.a.n cannot be cast to com.groupdocs.foundation.utils.wrapper.stream.GroupDocsInputStream
at com.groupdocs.conversion.examples.GroupDocsUtil.convertToBase64File(GroupDocsUtil.java:219)
at com.groupdocs.conversion.examples.TestMain2.main(TestMain2.java:51)
java.lang.ClassCastException: com.aspose.ms.System.a.a.n cannot be cast to com.groupdocs.foundation.utils.wrapper.stream.GroupDocsInputStream
at com.groupdocs.conversion.examples.GroupDocsUtil.convertToBase64File(GroupDocsUtil.java:219)
at com.groupdocs.conversion.examples.TestMain2.main(TestMain2.java:51)
java.lang.ClassCastException: com.aspose.ms.System.a.a.n cannot be cast to com.groupdocs.foundation.utils.wrapper.stream.GroupDocsInputStream
at com.groupdocs.conversion.examples.GroupDocsUtil.convertToBase64File(GroupDocsUtil.java:219)
at com.groupdocs.conversion.examples.TestMain2.main(TestMain2.java:51)
java.lang.ClassCastException: com.aspose.ms.System.a.a.n cannot be cast to com.groupdocs.foundation.utils.wrapper.stream.GroupDocsInputStream
at com.groupdocs.conversion.examples.GroupDocsUtil.convertToBase64File(GroupDocsUtil.java:219)

Please let us know how to resolve this.

Thanks

Hi Yogesh,


Thanks for taking interest in Next Generation GroupDocs.Conversion for Java.

Can you please provide us the problematic file as well?
We would also recommend you to go through our official GroupDocs.Conversion for Java example project and API documentation.

Many Thanks

Hi,


here are the 2 files.

Thanks

Hi Yogesh,


Thanks for providing us the sample files.
We shall investigate your issue and get back to you after completing the investigation. Please stay tuned.

Many Thanks

Hi,


can you please let us know the results of your analysis and what the fix is?

Thanks

Hi Yogesh,


Fortunately, we are able to spot a minor issue in your own code. With the code your provided, we got same exceptions as you mentioned earlier. Hence, here is the solution, you need to create a list of GroupDocsInputStream:
List gis = conversionHandler.<List> convert(fileName, imageSaveOptions);
and then get it’s 0th index:
b64is = new Base64InputStream(gis.get(0).toInputStream(),true,0,new byte[0]);
Rest of the code is working well. Hope it will resolve your issue. Please find the attached source files as well.

Have a nice weekend.
Many Thanks

Hi,


Thanks!! That solved my issue.

Hi Yogesh,


We are pleased to hear that.

Kind Regards