Thank you for your reply.
private static final Config CONFIG = new Config();
private static final ConversionHandler CONVERSION_HANDLER = new ConversionHandler(CONFIG);
@Override
public OutputStream getDocContent(byte[] contents, String fileName, int pageNumber) throws Exception {
long startTime = System.currentTimeMillis();
try {
return getPage(contents, fileName, pageNumber);
} catch (Exception e) {
logger.error(“Error occurred while getting contents of document: {}”, e.getMessage(), e);
throw new Exception(“Error occured while getting contents of the document”);
} finally {
long finish = System.currentTimeMillis();
logger.info(“Time taken to convert bytes (fileName: {}, page:{}) to image : {} ms”, fileName, pageNumber,
(finish - startTime));
}
}
private ByteArrayOutputStream getPage(byte[] fileContents, String fileName, int pageNum) throws Exception {
ByteArrayOutputStream bos = null;
ByteArrayInputStream bais = null;
ByteArrayInputStream result = null;
try {
bais = new ByteArrayInputStream(fileContents);
ImageSaveOptions options = new ImageSaveOptions();
options.setSaveFileType(FileType.PNG);
options.setResolution(120);
options.setPage(pageNum);
try {
result = CONVERSION_HANDLER.convertToImage(bais, fileName, options);
} catch (ConversionException e) {
throw e;
}
if (result != null) {
bos = new ByteArrayOutputStream();
try {
IOUtils.copy(result, bos);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
} finally {
try {
if (result != null) {
result.close();
}
} catch (IOException e) {
logger.error("Error occurred while closing stream: " + e.getMessage());
}
try {
if (bais != null) {
bais.close();
}
} catch (IOException e) {
logger.error("Error occurred while closing stream: " + e.getMessage());
}
}
return bos;
}