GroupDocs.Annotation allows you to generate document page previews. To do this, call the generatePreview method of the Document class.
Use the PreviewOptions class to manage the preview generation process: specify desired page numbers, image format, etc.
To generate a document preview, follow these steps:
Create an instance of the Annotator class. Specify the source document path as a parameter. The Document property of the Annotator class provides access to source document and implements generatePreview method.
Instantiate the PreviewOptions class. Follow these steps:
a. for each page, delegate stream creation (see the CreatePageStream event handler);
b. specify the image preview format - PNG / JPG / BMP;
c. specify page numbers to process;
d. set the custom size of preview images (if needed).
Note
Stream created by the CreatePageStream delegate are disposed automatically once after generation of preview image. To implement the custom image preview stream disposing, you have to specify an additional argument ReleasePageStream to clean up resources.
[PageNumbers]https://reference.groupdocs.com/annotation/java/com.groupdocs.annotation.options.pagepreview/previewoptions/#setPageNumbers-int—) - Page numbers that will be previewed;
PreviewFormat gets or sets the preview image format. Use the BMP format should be used for the best image quality. Use the JPG format in case of strict requirements to image size, but with lower quality than BMP. By default GroupDocs.Annotation uses the PNG format which is a golden mean between image quality and size.
RenderComments is an option to show or hide comments. By default it is true. Set it to false if you do not need to show comments. This property affects only on WordProcessing documents. The RenderComments value impacts any document comments.
RenderAnnotations is an option to show or hide annotations.By default it is true. Set it to false if you do not need to show annotations.
WorksheetColumns is a list of the WorksheetColumnsRange objects that indicates which columns to generate on specified worksheet.
The following code snippet demonstrates how to generate document previews.
Get document page previews
// This example demonstrates annotating generating previews from document
Annotatorannotator=newAnnotator("inputPath");// Create an instance of PreviewOptions class
PreviewOptionspreviewOptions=newPreviewOptions(newCreatePageStream(){@OverridepublicOutputStreaminvoke(intpageNumber){try{// Set several file name
StringfileName="OutputPath_"+pageNumber+".png";OutputStreamresult=newFileOutputStream(fileName);returnresult;}catch(Exceptionex){thrownewGroupDocsException(ex);}}});previewOptions.setPreviewFormat(PreviewFormats.PNG);previewOptions.setPageNumbers(newint[]{1,2,3});// Generate preview for documents
annotator.getDocument().generatePreview(previewOptions);
Set specific size for preview images
You can set a specific size for preview images. For example, you can create small-sized thumbnails and full-sized previews.
The following code snippet shows how to set specific size for preview images:
// This example demonstrates annotating generating previews from document
Annotatorannotator=newAnnotator("inputPath");// Create an instance of PreviewOptions class
PreviewOptionspreviewOptions=newPreviewOptions(newCreatePageStream(){@OverridepublicOutputStreaminvoke(intpageNumber){try{// Set several file name
StringfileName="OutputPath_"+pageNumber+".png";OutputStreamresult=newFileOutputStream(fileName);returnresult;}catch(Exceptionex){thrownewGroupDocsException(ex);}}});previewOptions.setPreviewFormat(PreviewFormats.PNG);previewOptions.setPageNumbers(newint[]{1,2,3,4});previewOptions.setHeight(100);previewOptions.setWidth(80);// Generate preview for documents
annotator.getDocument().generatePreview(previewOptions);
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.