Generate document pages preview

Overview

GroupDocs.Signature provides the PreviewOptions class to specify different options for managing document pages preview generation. The feature also supports archive previewing.

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

  • Create a new instance of the Signature class and pass the source document path as a constructor parameter.
  • Instantiate the PreviewOptions object with:
    • A delegate for each page stream creation (see event handler CreatePageStream);
    • Image preview format - PNG / JPG / BMP;
    • Page numbers to process;
    • Custom size for preview images (if needed).
Note
Streams created by the CreatePageStream delegate will be disposed of automatically after the preview image generation. If you need to implement custom image preview stream disposal, pass an additional argument ReleaseStream to clean up resources.

CreatePageStream delegate implementation

GroupDocs.Signature expects CreatePageStream delegate to obtain each page stream for image preview generation process

  function generatePreview() {
    // The path to the documents directory.
    const filePath = Constants.SAMPLE_PDF; // Assuming Constants.SAMPLE_PDF is defined elsewhere

    const signature = new signatureLib.Signature(filePath);

    // Create preview options object
    const previewOption = new signatureLib.PreviewOptions(createPageStream, releasePageStream);
    previewOption.setPreviewFormat(PreviewOptions.PreviewFormats.JPEG);

    // Generate preview
    signature.generatePreview(previewOption);
  }

  function createPageStream(pageNumber) {
    const imageFilePath = path.join(Constants.OutputPath, 'GeneratePreviewFolder', `image-${pageNumber}.jpg`);
    const folder = path.dirname(imageFilePath);

    if (!fs.existsSync(folder)) {
      fs.mkdirSync(folder, { recursive: true });
    }

    return fs.createWriteStream(imageFilePath);
  }

  function releasePageStream(pageNumber, pageStream) {
    pageStream.end();
    const imageFilePath = path.join(Constants.OutputPath, 'GeneratePreviewFolder', `image-${pageNumber}.jpg`);
    console.log(`Image file ${imageFilePath} is ready for preview`);
  }

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.