Generate document preview
Leave feedback
If you need to implement a UI for your application it can be useful to generate image previews for a document the users are going to work with. Such previews can be easily displayed on a web page or in an appropriate component of a desktop/mobile application. The code sample below demonstrates how to generate image previews for certain document pages.
Here are the steps to generate a document preview for a particular page:
- Load a document to preview
- Specify a delegate that will be used to create the page streams (please see the ICreatePageStream interface for more information)
- Specify the page numbers
- Generate previews for desired pages using the generatePreview method of the Metadata class
basic_usage.GenerateFilePreview
public class GenerateFilePreview {
public static void run() {
try (Metadata metadata = new Metadata(Constants.InputDocx)) {
PreviewOptions previewOptions = new PreviewOptions(new GenerateFilePreview().new CreatePageStream());
previewOptions.setPreviewFormat(PreviewFormats.PNG);
previewOptions.setPageNumbers(new int[]{1});
metadata.generatePreview(previewOptions);
}
}
private class CreatePageStream implements ICreatePageStream {
@Override
public OutputStream createPageStream(int pageNumber) {
File file = new File(String.format("%s\\result_%s.png", Constants.OutputPath, pageNumber));
OutputStream stream = null;
try {
stream = new FileOutputStream(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
return stream;
}
}
}
To learn more about library features and get familiar how to manage metadata and more, please refer to theadvanced usage section.
You may easily run the code above and see the feature in action in our GitHub examples:
Along with a full featured Java library we provide simple, but powerful free Apps.
You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.
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.