Generate document pages preview

GroupDocs.Comparison allows you to generate page previews for source, target and output document(s) using 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 the document preview using the GroupDocs.Comparison API, follow these steps:

  1. Create an instance of the Comparer class. Specify the source document path as a constructor parameter.
  2. Add the target document to comparison using the add() method.
  3. The getSource() and getTargets() methods of the Comparer object allow you to access source and target documents and the generatePreview() method.
  4. Instantiate the PreviewOptions object. Specify the following parameters:
    • delegate for each page stream creation (see the reatePageStreamFunction event handler)
    • image preview format - PNG / JPG / BMP
    • page numbers to process
    • custom size of preview images (if needed)
      Note
      Stream created by the CreatePageStreamFunction delegate is automatically disposed when the preview image is generated. If you need to implement the custom disposing of the image preview stream, specify the ReleasePageStreamFunction argument to clean up resources.
  5. Call the generatePreview() method of the Source and Targets documents. Specify the PreviewOptions.

The PreviewOptions class’s main methods are as follows:

  • getCreatePageStreamFunction() returns a delegate that defines a method to create the output page preview stream
  • getReleasePageStreamFunction() returns a delegate that defines a method to remove the output page preview stream. This can be used when you need advanced control of resource handling
  • setWidth sets the preview image width. Use this method to customize the output image width
  • setHeight sets the preview image height. Use this method to customize output image height
  • setPageNumbers defines an array of page numbers to be previewed;
  • setPreviewFormat sets the preview image format which provides an ability to choose between image quality and size. Use the BMP format for the best image quality. Use the JPG format to produce the smallest image size (and faster loading image previews) but with lower quality than BMP. By default, GroupDocs.Comparison uses the PNG format to provide appropriate image quality and size.

The following code snippet shows how to generate document previews:

Get page previews for source document

const comparer = new groupdocs.comparison.Comparer(sourceFile);
const previewOptions = new groupdocs.comparison.PreviewOptions(
  pageNumber => Files.newOutputStream(previewDirectory + "result_" + pageNumber + ".png")
);
previewOptions.setPreviewFormat(groupdocs.comparison.PreviewFormats.PNG);
previewOptions.setPageNumbers(new int[]{1, 2});
comparer.getSource().generatePreview(previewOptions);

The result is as follows:

Get page previews for the target document

const comparer = new groupdocs.comparison.Comparer(sourceFile);
comparer.add(targetFile);
const previewOptions = new groupdocs.comparison.PreviewOptions(
  pageNumber => Files.newOutputStream(previewDirectory + "result_" + pageNumber + ".png")
);
previewOptions.setPreviewFormat(groupdocs.comparison.PreviewFormats.PNG);
previewOptions.setPageNumbers(new int[]{1, 2});
comparer.getTargets().get(0).generatePreview(previewOptions);

Get page previews for the output document

const comparer = new groupdocs.comparison.Comparer(sourceFile);
comparer.add(targetFile);
const resultPath = comparer.compare(outputFile);
const document = new groupdocs.comparison.Document(resultPath);
const previewOptions = new groupdocs.comparison.PreviewOptions(
  pageNumber => Files.newOutputStream(previewDirectory + "result_" + pageNumber + ".png")
);
previewOptions.setPreviewFormat(groupdocs.comparison.PreviewFormats.PNG);
previewOptions.setPageNumbers(new int[]{1, 2});
document.generatePreview(previewOptions);

Set specific sizes for preview images

const comparer = new groupdocs.comparison.Comparer(sourceFile);
comparer.add(targetFile);
const resultPath = comparer.compare(outputFile);
const document = new groupdocs.comparison.Document(resultPath);

const previewOptions = new groupdocs.comparison.PreviewOptions(
  pageNumber => Files.newOutputStream(previewDirectory + "result-SetSpecificImagesSize_" + pageNumber + ".png")
);
previewOptions.setPreviewFormat(groupdocs.comparison.PreviewFormats.PNG);
previewOptions.setPageNumbers(new int[]{1, 2});
previewOptions.setHeight(1000);
previewOptions.setWidth(1000);
document.generatePreview(previewOptions);
Note
This feature is not supported for WordProcessing documents.

Get page previews and clean resources manually

const comparer = new groupdocs.comparison.Comparer(sourceFile);
comparer.add(targetFile);
const resultPath = comparer.compare(outputFile);
const document = new groupdocs.comparison.Document(resultPath);

const previewOptions = new groupdocs.comparison.PreviewOptions(
  pageNumber => Files.newOutputStream(previewDirectory + "result-GetPagePreviewsResouresCleaning_" + pageNumber + ".png")
);
previewOptions.setPreviewFormat(groupdocs.comparison.PreviewFormats.PNG);
previewOptions.setPageNumbers(new int[]{1, 2});
previewOptions.setReleasePageStreamFunction(new groupdocs.comparison.ReleasePageStreamFunction() {
    @Override
    public void invoke(int pageNumber, OutputStream outputStream) {
        System.out.println("Releasing memory for page: " + pageNumber);
        outputStream.close();
    }
});
document.generatePreview(previewOptions);