The following example shows how to generate document page previews:
// Create an instance of Parser class to generate document page previews
try(Parserparser=newParser(Constants.SamplePdfWithToc)){// Create preview options
PreviewOptionspreviewOptions=newPreviewOptions(newICreatePageStream(){@OverridepublicOutputStreamcreatePageStream(intpageNumber){try{returnnewFileOutputStream(getOutputPath(String.format("preview_%d.png",pageNumber)));}catch(IOExceptionex){thrownewRuntimeException(ex.getMessage());}}});// Set PNG as an output image format
previewOptions.setPreviewFormat(PreviewFormats.Png);// Set DPI for the output image
previewOptions.setDpi(72);// Generate previews
parser.generatePreview(previewOptions);}privatestaticStringgetOutputPath(StringfileName)throwsjava.io.IOException{returnConstants.getOutputFilePath(fileName);}
Note
Spreadsheets are rendered by tiles. See the example bellow.
Here are the steps to generate spreadsheets page previews:
The following example shows how to generate spreadsheet page previews:
// Create an instance of Parser class to generate spreadsheet page previews
try(Parserparser=newParser(Constants.SampleXlsx)){finalPageRenderInfo[]renderInfo={null};// Create preview options
PreviewOptionspreviewOptions=newPreviewOptions(newICreatePageStream(){@OverridepublicOutputStreamcreatePageStream(intpageNumber){try{returnnewFileOutputStream(getOutputPath(renderInfo[0],pageNumber));}catch(IOExceptionex){thrownewRuntimeException(ex.getMessage());}}});// Set delegate to obtain the render info
previewOptions.setPreviewPageRender(newIPreviewPageRender(){@OverridepublicvoidpreviewPageRender(PageRenderInfopageRenderInfo){renderInfo[0]=pageRenderInfo;}});// Set PNG as an output image format
previewOptions.setPreviewFormat(PreviewFormats.Png);// Set DPI for the output image
previewOptions.setDpi(72);// Generate previews
parser.generatePreview(previewOptions);}privatestaticStringgetOutputPath(PageRenderInforenderInfo,intpageNumber)throwsjava.io.IOException{// Set the file name. If the render info is set, then tile name is {Row}x{Column}.png
StringfileName=renderInfo==null?String.format("preview_%d.png",pageNumber):String.format("%d\\%dx%d.png",renderInfo.getPageNumber(),renderInfo.getRow(pageNumber),renderInfo.getColumn(pageNumber));returnConstants.getOutputFilePath(fileName);}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.