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). 

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

Comparer comparer = new Comparer("C:\\source.pdf");
try {
    PreviewOptions previewOptions = new PreviewOptions(new Delegates.CreatePageStream() {
        @Override
        public OutputStream invoke(int pageNumber) {
            String pagePath = "C:\\"+ "result_" + pageNumber + ".png";
            try {
                return new FileOutputStream(pagePath);
            } catch (FileNotFoundException e) {
                // Process exception
                throw new RuntimeException(e);
            }
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    comparer.getSource().generatePreview(previewOptions);
} finally {
    comparer.dispose();
}

Get page previews for target document

Comparer comparer = new Comparer("C:\\source.pdf");
comparer.add("C:\\target.pdf");
try {
    PreviewOptions previewOptions = new PreviewOptions(new Delegates.CreatePageStream() {
        @Override
        public OutputStream invoke(int pageNumber) {
            String pagePath = "C:\\"+ "result_" + pageNumber + ".png";
            try {
                return new FileOutputStream(pagePath);
            } catch (FileNotFoundException e) {
                // Process exception
                throw new RuntimeException(e);
            }
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    comparer.getTargets().get(0).generatePreview(previewOptions);
} finally {
    comparer.dispose();
}

Get page previews for resultant document

Comparer comparer = new Comparer("C:\\source.pdf");
try {
    comparer.add("C:\\target.pdf");
    comparer.compare("C:\\target.pdf");
    Document document = new Document("C:\\result.pdf");
    PreviewOptions previewOptions = new PreviewOptions(new Delegates.CreatePageStream() {
        @Override
        public OutputStream invoke(int pageNumber) {
            String pagePath = "C:\\"+ "result_" + pageNumber + ".png";
            try {
                return new FileOutputStream(pagePath);
            } catch (FileNotFoundException e) {
                // Process exception
                throw new RuntimeException(e);
            }
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    document.generatePreview(previewOptions);
} finally {
    comparer.dispose();
}

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.

Comparer comparer = new Comparer("C:\\source.pdf");
try {
    comparer.add("C:\\target.pdf");
    comparer.compare(new FileOutputStream("C:\\result.pdf"));
    Document document = new Document("C:\\result.pdf");

    PreviewOptions previewOptions = new PreviewOptions(new Delegates.CreatePageStream() {
        @Override
        public OutputStream invoke(int pageNumber) {
            String pagePath = "C:\\"+ "result-SetSpecificImagesSize_" + pageNumber + ".png";
            try {
                return new FileOutputStream(pagePath);
            } catch (FileNotFoundException e) {
                // Process exception
                throw new RuntimeException(e);
            }
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    previewOptions.setHeight(1000);
    previewOptions.setWidth(1000);
    document.generatePreview(previewOptions);
} finally {
    comparer.dispose();
}

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.

// Method should match with ReleasePageStream interface signature
private static void userReleaseStreamMethod(int pageNumber, OutputStream stream) {
    System.out.println("Releasing memory for page: " + pageNumber);
    stream.close();
}
 
// Somewhere in the same class
Comparer comparer = new Comparer("C:\\source.pdf");
try {
    comparer.add("C:\\target.pdf");
    comparer.compare("C:\\result.pdf");
    Document document = new Document("C:\\result.pdf");

    PreviewOptions previewOptions = new PreviewOptions(new Delegates.CreatePageStream() {
        @Override
        public OutputStream invoke(int pageNumber) {
            String pagePath = "C:\\"+ "result-GetPagePreviewsResouresCleaning_" + pageNumber + ".png";
            try {
                return new FileOutputStream(pagePath);
            } catch (FileNotFoundException e) {
                // Process exception
                throw new RuntimeException(e);
            }
        }
    });
    previewOptions.setPreviewFormat(PreviewFormats.PNG);
    previewOptions.setPageNumbers(new int[]{1, 2});
    previewOptions.setReleasePageStream(new Delegates.ReleasePageStream() {
        @Override
        public void invoke(int pageNumber, OutputStream outputStream) {
            userReleaseStreamMethod(pageNumber, outputStream);
        }
    });
    document.generatePreview(previewOptions);
} finally {
    comparer.dispose();
}

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.