Generate document pages preview

GroupDocs.Comparison allows to generate page previews for source, target and resultant document(s) using generatePreview method of a Document class.

PreviewOptions class is used to manage preview generation process - specify desired page numbers, image format etc.

The following are the steps to generate document preview with GroupDocs.Comparison API:

  • Create new instance of Comparer class and pass source document path as a constructor parameter.
  • Add target document(s) to comparison using add method.
  • Source and Targets properties of Comparer object allows to access source and target documents and provides generatePreview method.
  • Instantiate the PreviewOptions object with:
    • callback for each page stream creation (see event handler CreatePageStream);
    • image preview format - PNG / JPG / BMP,
    • page numbers to process;
    • custom size of preview images (if needed).
Note
Stream that were created by createPageStream callback will be disposed automatically once after generation of preview image. If you need to implement custom image preview stream disposing you have to pass additional argument ReleasePageStream to clean up resources.

Here a PreviewOptions class main properties:

  • CreatePageStream - Callback which defines method to create output page preview stream;
  • ReleasePageStream - Callback which defines method to remove output page preview stream. This is can be used when need advanced control for resources handling.
  • Width - Preview image width. This property used when need customize output image width;
  • Height - Preview image height. This property used when need customize output image height;
  • PageNumbers - Page numbers that will be previewed;
  • PreviewFormat - Gets or sets the preview image format which provides ability to choose between image quality and size. BMP format should be used for the best image quality. JPG format will be useful in case of strict requirements to image size - it produces smallest image size (and faster loading image previews), but with lower quality than BMP. By default PNG format is selected - which is a golden mean between image quality and size.

The following code snippet demonstrates how to generate document previews.

Get page previews for source document

Get page previews for target document

Get page previews for resultant document

Set specific size for preview images

In some cases it may be useful to set specific image size during document pages preview generation. For example, to generate document pages thumbnails - small images that is a compressed preview image of the original image that is used as a placeholder. The main advantage of such thumbnail images is their reduced file size compared to the original page image.

The following code snippet demonstrates how to set specific size for preview images.

Note
NOTE: This feature is not supported for WordProcessing documents yet.

Get page previews with manual resource cleaning

By default, after generating and rendering document page preview image stream will be immediately disposed. However, there is an ability to implement custom method for handling this operation.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple, but powerful free Apps. You are welcome to compare your DOC or DOCX, XLS or XLSX, PPT or PPTX, PDF, EML, EMLX, MSGand other documents with free to use online GroupDocs Comparison App.