Generate document page preview

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:

  1. 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.
  2. 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.
  3. Call the generatePreview method of the Document class. Specify PreviewOptions.

The PreviewOptions class main properties are as follows:

  • CreatePageStream is a delegate which defines the method to create output page preview stream.
  • ReleasePageStream is a delegate which defines the method to remove output page preview stream. Use it for advanced control for resources handling.
  • Width specifies the preview image width.
  • Height specifies the preview image height.
  • [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
Annotator annotator = new Annotator("inputPath");

// Create an instance of PreviewOptions class
PreviewOptions previewOptions = new PreviewOptions(new CreatePageStream() {
    @Override
    public OutputStream invoke(int pageNumber) {
        try {
            // Set several file name
            String fileName = "OutputPath_"+pageNumber+".png";
            OutputStream result = new FileOutputStream(fileName);
            return result;
        } catch (Exception ex) {
            throw new GroupDocsException(ex);
        }
    }
});

previewOptions.setPreviewFormat(PreviewFormats.PNG);

previewOptions.setPageNumbers(new int[]{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
Annotator annotator = new Annotator("inputPath");

// Create an instance of PreviewOptions class
PreviewOptions previewOptions = new PreviewOptions(new CreatePageStream() {
    @Override
    public OutputStream invoke(int pageNumber) {
        try {
            // Set several file name
            String fileName = "OutputPath_"+pageNumber+".png";
            OutputStream result = new FileOutputStream(fileName);
            return result;
        } catch (Exception ex) {
            throw new GroupDocsException(ex);
        }
    }
});

previewOptions.setPreviewFormat(PreviewFormats.PNG);
previewOptions.setPageNumbers(new int[] { 1, 2, 3, 4 });
previewOptions.setHeight(100);
previewOptions.setWidth(80);

// Generate preview for documents
annotator.getDocument().generatePreview(previewOptions);