Generating document preview - advanced

GroupDocs.Signature provides PreviewOptions class to specify different options to manage document pages preview generation process. Since 20.3 version there’s ability to hide signatures from documents. Using property setHideSignature of PreviewOptions will allow to hide signatures from document preview.

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

  • Create new instance of Signature class and pass source document path as a constructor parameter.

  • Instantiate the PreviewOptions object with:

    • delegate for each page stream creation (see PageStreamFactory); 

    • property setHideSignature set to true

    • image preview format - PNG / JPG / BMP, 

    • page numbers to process;

    • custom size of preview images (if needed). 

      Note
      Stream that were created by createPageStream delegate will be disposed automatically once after generation of preview image. If you need to implement custom image preview stream disposing you have to pass additional argument closePageStream to clean up resources.
  • Call generatePreview method of Signature class instance and pass PreviewOptions to it.

Generate document preview without signatures on it

public static void getPreview()
{
    // The path to the documents directory.
    String filePath = "C:\signed.pdf";
 
    Signature signature = new Signature(filePath);
    try
    {
        // create preview options object
        PreviewOptions previewOption = new PreviewOptions(new PageStreamFactory() {
            @Override
            public OutputStream createPageStream(int pageNumber) {
                return generateStream(pageNumber);
            }
 
            @Override
            public void closePageStream(int pageNumber, OutputStream pageStream) {
                releasePageStream(pageNumber, pageStream);
            }
        });
        previewOption.setPreviewFormat(PreviewFormats.JPEG);
        previewOption.setHideSignatures(true);
        // generate preview
        signature.generatePreview(previewOption);
 
    }catch (Exception e){
        throw new GroupDocsSignatureException(e.getMessage());
    }
}
 
 
private static OutputStream generateStream(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());
    }
 
}
 
private static void releasePageStream(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());
    }
}

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