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 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 allows 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 disposed automatically when 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 main methods are as follows:

  • getCreatePageStreamFunction() returns a delegate which defines method to create output page preview stream
  • getReleasePageStreamFunction() returns a delegate which defines method to remove output page preview stream. This is can be used when need advanced control for resources handling
  • setWidth sets the preview image width. Use this method to customize 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 ability to choose between image quality and size. Use the BMP format for the best image quality. Use the JPG format to produce 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

try (Comparer comparer = new Comparer(sourceFile)) {
    PreviewOptions previewOptions = new PreviewOptions(new CreatePageStreamFunction() {
        @Override
        public OutputStream invoke(int pageNumber) {
            return new FileOutputStream(previewDirectory + "result_" + pageNumber + ".png");
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    comparer.getSource().generatePreview(previewOptions);
}

The result is as follows:

Get page previews for target document

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    PreviewOptions previewOptions = new PreviewOptions(new CreatePageStreamFunction() {
        @Override
        public OutputStream invoke(int pageNumber) {
            return new FileOutputStream(previewDirectory + "result_" + pageNumber + ".png");
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    comparer.getTargets().get(0).generatePreview(previewOptions);
}

Get page previews for output document

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    final Path resultPath = comparer.compare(outputFile);
    Document document = new Document(resultPath);
    PreviewOptions previewOptions = new PreviewOptions(new CreatePageStreamFunction() {
        @Override
        public OutputStream invoke(int pageNumber) {
            return new FileOutputStream(previewDirectory + "result_" + pageNumber + ".png");
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    document.generatePreview(previewOptions);
}

Set specific size for preview images

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    final Path resultPath = comparer.compare(outputFile);
    Document document = new Document(resultPath);
    
    PreviewOptions previewOptions = new PreviewOptions(new CreatePageStreamFunction() {
        @Override
        public OutputStream invoke(int pageNumber) {
            return new FileOutputStream(previewDirectory + "result-SetSpecificImagesSize_" + pageNumber + ".png");
        }
    });
    previewOptions.setPreviewFormat(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

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    final Path resultPath = comparer.compare(outputFile);
    Document document = new Document(resultPath);
    
    PreviewOptions previewOptions = new PreviewOptions(new CreatePageStreamFunction() {
        @Override
        public OutputStream invoke(int pageNumber) {
            return new FileOutputStream(previewDirectory + "result-GetPagePreviewsResouresCleaning_" + pageNumber + ".png");
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    previewOptions.setReleasePageStreamFunction(new ReleasePageStreamFunction() {
        @Override
        public void invoke(int pageNumber, OutputStream outputStream) {
            System.out.println("Releasing memory for page: " + pageNumber);
            outputStream.close();
        }
    });
    document.generatePreview(previewOptions);
}