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.
The getSource() and getTargets() methods of the Comparer object allow you to access source and target documents and the generatePreview() method. When creating PreviewOptions, you need to specify:
delegate for each page stream creation (see the CreatePageStreamFunction 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.
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 snippets show how to generate document previews in different scenarios.
Get page previews for source document
The following example generates PNG previews for the first two pages of the source document.
'use strict';// Import GroupDocs.Comparison for Node.js via Java and Java interop classes
constgroupdocs=require('@groupdocs/groupdocs.comparison');constjava=require('java');constInputStream=java.import('java.io.FileInputStream');constFileOutputStream=java.import('java.io.FileOutputStream');// Open the source document as a Java input stream
constsourceFile=newInputStream('sample-files/source.docx');// Initialize the Comparer using the source stream
constcomparer=newgroupdocs.Comparer(sourceFile);// Define how each preview image stream should be created
constcreatePageStream=java.newProxy('com.groupdocs.comparison.common.function.CreatePageStreamFunction',{invoke:function(pageNumber){// Create a separate PNG file for every generated page preview
returnnewFileOutputStream('result_'+pageNumber+'.png');}});// Configure preview options: format and page numbers to render
constpreviewOptions=newgroupdocs.PreviewOptions(createPageStream);previewOptions.setPreviewFormat(groupdocs.PreviewFormats.PNG);previewOptions.setPageNumbers(java.newArray('int',[1,2]));// Generate preview images for the source document
comparer.getSource().generatePreview(previewOptions);// Terminate the process with a success exit code
process.exit(0);
This example demonstrates how to generate page previews for the source document. It creates a Comparer instance with a source document stream, then creates a CreatePageStreamFunction proxy that creates a separate PNG file for each page preview. The PreviewOptions object is configured with PNG format and page numbers [1, 2], and generatePreview() is called on the source document accessed via getSource(). This generates preview images for the first two pages of the source document.
The result is as follows:
Get page previews for the target document
The following example compares two documents and generates previews for the first target document.
'use strict';// Import GroupDocs.Comparison and Java NIO classes for writing output streams
constgroupdocs=require('@groupdocs/groupdocs.comparison');constjava=require('java');constInputStream=java.import('java.io.FileInputStream');constFiles=java.import('java.nio.file.Files');constPaths=java.import('java.nio.file.Paths');// Load source and target documents as input streams
constsource=newInputStream('sample-files/source.docx');consttarget=newInputStream('sample-files/target.docx');// Create a Comparer instance for the source file
constcomparer=newgroupdocs.Comparer(source);// Add the target document to the comparison set
comparer.add(target);// Configure preview generation (create PNG stream per page)
constcreatePageStream=java.newProxy('com.groupdocs.comparison.common.function.CreatePageStreamFunction',{invoke:function(pageNumber){returnFiles.newOutputStream(Paths.get('result_'+pageNumber+'.png'));}});constpreviewOptions=newgroupdocs.PreviewOptions(createPageStream);previewOptions.setPreviewFormat(groupdocs.PreviewFormats.PNG);previewOptions.setPageNumbers(java.newArray('int',[1,2]));// Generate preview images for the first target document in the comparer
comparer.getTargets().get(0).generatePreview(previewOptions);// Exit the process
process.exit(0);
This example compares two documents first, then generates previews for the target document. It creates a Comparer instance with source and target streams, performs the comparison, and then accesses the first target document using getTargets().get(0). It creates a PreviewOptions object with a stream creation function that uses Java NIO’s Files.newOutputStream() and Paths.get() to create output streams, then calls generatePreview() on the target document to generate preview images.
Get page previews for the output document
The following example first produces a comparison result document and then generates previews from that output file.
'use strict';// Import GroupDocs.Comparison and Java NIO classes for file I/O
constgroupdocs=require('@groupdocs/groupdocs.comparison');constjava=require('java');constInputStream=java.import('java.io.FileInputStream');constFiles=java.import('java.nio.file.Files');constPaths=java.import('java.nio.file.Paths');// Define paths for input and output documents
constsourceFilePath='sample-files/source.docx';consttargetFilePath='sample-files/target.docx';constoutputFilePath='result.docx';// Open source and target documents as streams
constsourceStream=newInputStream(sourceFilePath);consttargetStream=newInputStream(targetFilePath);// Compare source and target, saving the merged result to disk
constcomparer=newgroupdocs.Comparer(sourceStream);comparer.add(targetStream);comparer.compare(outputFilePath);// Open the generated output document as a GroupDocs Document instance
constdocument=newgroupdocs.Document(outputFilePath);// Configure preview generation for the result document
constcreatePageStream=java.newProxy('com.groupdocs.comparison.common.function.CreatePageStreamFunction',{invoke:function(pageNumber){returnFiles.newOutputStream(Paths.get('result_'+pageNumber+'.png'));}});constpreviewOptions=newgroupdocs.PreviewOptions(createPageStream);previewOptions.setPreviewFormat(groupdocs.PreviewFormats.PNG);constpageNumbersArray=java.newArray('int',[1,2]);previewOptions.setPageNumbers(pageNumbersArray);// Generate preview images for the final comparison document
document.generatePreview(previewOptions);// Exit the process
process.exit(0);
This example first performs a comparison and saves the result document, then generates previews from that output file. It creates a Comparer, performs the comparison, and saves the result. Then it creates a Document instance from the output file path and generates previews from that document. This approach allows you to generate previews of the final comparison result document, showing how the merged document with all changes highlighted appears.
Set specific sizes for preview images
The following example sets a custom width and height for the generated preview images.
'use strict';// Import GroupDocs.Comparison and Java I/O for creating image files
constgroupdocs=require('@groupdocs/groupdocs.comparison');constjava=require('java');constInputStream=java.import('java.io.FileInputStream');constFileOutputStream=java.import('java.io.FileOutputStream');// Define paths for input and output documents
constsourcePath='sample-files/source.docx';consttargetPath='sample-files/target.docx';constoutputPath='result.docx';// Open documents as streams and run comparison
constsourceStream=newInputStream(sourcePath);consttargetStream=newInputStream(targetPath);constcomparer=newgroupdocs.Comparer(sourceStream);comparer.add(targetStream);comparer.compare(outputPath);// Open the resulting document for preview generation
constdocument=newgroupdocs.Document(outputPath);// Configure how preview images will be written
constcreatePageStream=java.newProxy('com.groupdocs.comparison.common.function.CreatePageStreamFunction',{invoke:function(pageNumber){returnnewFileOutputStream('result-'+pageNumber+'.png');}});// Create preview options with a custom size
constpreviewOptions=newgroupdocs.PreviewOptions(createPageStream);previewOptions.setPreviewFormat(groupdocs.PreviewFormats.PNG);previewOptions.setPageNumbers(java.newArray('int',[1,2]));previewOptions.setHeight(1000);// target image height in pixels
previewOptions.setWidth(1000);// target image width in pixels
// Generate resized preview images
document.generatePreview(previewOptions);// Exit the process
process.exit(0);
This example demonstrates how to set custom dimensions for preview images. After performing the comparison and creating a Document instance from the result, it creates PreviewOptions and sets both setHeight(1000) and setWidth(1000) to generate preview images with specific pixel dimensions. This allows you to control the size of the generated preview images, which can be useful for creating thumbnails or ensuring consistent image sizes.
Note
This feature is not supported for WordProcessing documents.
Get page previews and clean resources manually
The following example demonstrates how to plug in a custom release handler to manually dispose preview streams.
'use strict';// Import GroupDocs.Comparison and Java stream classes
constgroupdocs=require('@groupdocs/groupdocs.comparison');constjava=require('java');constInputStream=java.import('java.io.FileInputStream');constFileOutputStream=java.import('java.io.FileOutputStream');// Open source and target files as streams
constsourceFile=newInputStream('sample-files/source.docx');consttargetFile=newInputStream('sample-files/target.docx');// Run comparison and obtain the path of the output document
constoutputFile='result.docx';constcomparer=newgroupdocs.Comparer(sourceFile);comparer.add(targetFile);constresultPath=comparer.compare(outputFile);// Wrap the result document for preview generation
constdocument=newgroupdocs.Document(resultPath);// Create page stream factory for preview images
constcreatePageStream=java.newProxy('com.groupdocs.comparison.common.function.CreatePageStreamFunction',{invoke:function(pageNumber){returnnewFileOutputStream('result-page-'+pageNumber+'.png');}});// Define custom cleanup logic for created streams
constreleasePageStream=java.newProxy('com.groupdocs.comparison.common.function.ReleasePageStreamFunction',{invoke:function(pageNumber,outputStream){console.log('Releasing memory for page: ',pageNumber);outputStream.close();// close stream explicitly when GroupDocs is done with it
}});// Use both factory and release handlers in preview options
constpreviewOptions=newgroupdocs.PreviewOptions(createPageStream,releasePageStream);previewOptions.setPreviewFormat(groupdocs.PreviewFormats.PNG);previewOptions.setPageNumbers(java.newArray('int',[1,2]));// Generate page previews with custom resource management
document.generatePreview(previewOptions);// Exit the process
process.exit(0);
This example demonstrates advanced resource management for preview generation. It creates both a CreatePageStreamFunction proxy to create output streams for each page preview and a ReleasePageStreamFunction proxy to manually clean up resources when preview generation is complete. The ReleasePageStreamFunction logs a message and explicitly closes the stream. Both functions are passed to the PreviewOptions constructor, giving you full control over stream lifecycle management, which is useful for memory-intensive operations or when you need custom cleanup logic.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.