Generate document pages preview
Leave feedback
On this page
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:
- Create an instance of the Comparer class. Specify the source document path as a constructor parameter.
- Add the target document to comparison using the add() method.
- The getSource() and getTargets() methods of the Comparer object allows you to access source and target documents and the generatePreview() method.
- 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)NoteStream 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.
- 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:
import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.common.function.CreatePageStreamFunction;
import com.groupdocs.comparison.options.PreviewOptions;
import com.groupdocs.comparison.options.enums.PreviewFormats;
import java.io.FileOutputStream;
import java.io.OutputStream;
// ...
try (Comparer comparer = new Comparer("source.docx")) {
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:
import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.common.function.CreatePageStreamFunction;
import com.groupdocs.comparison.options.PreviewOptions;
import com.groupdocs.comparison.options.enums.PreviewFormats;
import java.io.FileOutputStream;
import java.io.OutputStream;
// ...
try (Comparer comparer = new Comparer("source.docx")) {
comparer.add("target.docx");
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);
}
import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.Document;
import com.groupdocs.comparison.common.function.CreatePageStreamFunction;
import com.groupdocs.comparison.options.PreviewOptions;
import com.groupdocs.comparison.options.enums.PreviewFormats;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
// ...
try (Comparer comparer = new Comparer("source.docx")) {
comparer.add("target.docx");
final Path resultPath = comparer.compare("result.docx");
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);
}
import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.Document;
import com.groupdocs.comparison.common.function.CreatePageStreamFunction;
import com.groupdocs.comparison.options.PreviewOptions;
import com.groupdocs.comparison.options.enums.PreviewFormats;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
// ...
try (Comparer comparer = new Comparer("source.docx")) {
comparer.add("target.docx");
final Path resultPath = comparer.compare("result.docx");
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);
}
NoteThis feature is not supported for WordProcessing documents.
import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.Document;
import com.groupdocs.comparison.common.function.CreatePageStreamFunction;
import com.groupdocs.comparison.common.function.ReleasePageStreamFunction;
import com.groupdocs.comparison.options.PreviewOptions;
import com.groupdocs.comparison.options.enums.PreviewFormats;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
// ...
try (Comparer comparer = new Comparer("source.docx")) {
comparer.add("target.docx");
final Path resultPath = comparer.compare("result.docx");
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);
}
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.