Generate document pages preview

GroupDocs.Signature provides PreviewOptions class to specify different options to manage document pages preview generation process.

Here are the steps to generate document preview with GroupDocs.Signature:

  • Create new instance of Signature class and pass source document path as a constructor parameter.
  • Instantiate the PreviewOptions object with: 
    • interface for each page stream creation (see PageStreamFactory); 
    • image preview format - PNG / JPG / BMP, 
    • page numbers to process;
    • custom size of preview images (if needed). 
  • Call generatePreview method of Signature class instance and pass PreviewOptions to it. 

Generate document preview from file on local disk

   
	// instantiating the signature object
    final Signature signature = new Signature("sample.pdf");
     
	PreviewOptions previewOption = new PreviewOptions("C:\\GeneratePreviewHideSignatures\\image.jpg", 0);
	previewOptions.setPreviewFormat(PreviewFormats.PNG);
	signature.generatePreview(previewOptions);
    

createPageStream method implementation

GroupDocs.Signature expects createPageStream method to obtain each page stream for image preview generation process

@Override
public OutputStream createPageStream(int pageNumber) {
    try {
        Path path = Paths.get("C:\\GeneratePreviewFolder");

        if (!Files.exists(path)) {
            Files.createDirectory(path);
            System.out.println("Directory created");
        } else {

            System.out.println("Directory already exists");
        }
        File filePath = new File(path+"\\image-"+pageNumber+".jpg");

        return new FileOutputStream(filePath);
    }catch (Exception e){
        throw new GroupDocsSignatureException(e.getMessage());
    }
}

ClosePageStream method implementation

@Override
public void closePageStream(int pageNumber, OutputStream pageStream) {
    try {
        pageStream.close();
        String imageFilePath = new File("GeneratePreviewFolder", "image-" +pageNumber +  ".jpg").getPath();
        System.out.print("Image file "+imageFilePath+" is ready for preview");
    }catch (Exception e){
        throw new GroupDocsSignatureException(e.getMessage());
    }
}

Generate document preview from stream with custom stream creating and closing methods

// instantiating the signature object
	FileInputStream stream = new FileInputStream("sample.pdf")
    final Signature signature = new Signature(stream);
        // Image from specified page
	PreviewOptions previewOption = new PreviewOptions(new PageStreamFactory() {
    @Override
    public OutputStream createPageStream(int pageNumber) {
        try {
            Path path = Paths.get("C:\\GeneratePreviewHideSignatures");

            if (!Files.exists(path)) {

                Files.createDirectory(path);
                System.out.println("Directory created");
            } else {

                System.out.println("Directory already exists");
            }
            File filePath = new File(path+"\\image-"+pageNumber+".jpg");

            return new FileOutputStream(filePath);
        }catch (Exception e){
            throw new GroupDocsSignatureException(e.getMessage());
        }
    }

    @Override
    public void closePageStream(int pageNumber, OutputStream pageStream) {
        try {
            pageStream.close();
            String imageFilePath = new File("C:\\GeneratePreviewHideSignatures", "image-" +pageNumber +  ".jpg").getPath();
            System.out.print("Image file "+imageFilePath+" is ready for preview");
        }catch (Exception e){
            throw new GroupDocsSignatureException(e.getMessage());
        }
    }
});
	previewOptions.setPreviewFormat(PreviewFormats.PNG);
	signature.generatePreview(previewOptions);


More resources 

Advanced Usage Topics 

To learn more about document eSign features, please refer to the advanced usage section.

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 .NET library we provide simple, but powerful free Apps.
You are welcome to eSign PDF, Word, Excel, PowerPoint documents with free to use online GroupDocs Signature App.