Generate document pages preview

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Signature MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-signature -- dnx GroupDocs.Signature.Mcp --yes
Folders, licensing and more →

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.