Document 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.Watermark 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-watermark -- dnx GroupDocs.Watermark.Mcp --yes
Folders, licensing and more →

GroupDocs.Watermark library equipped with the capability to generate preview images for every page of your documents, simplifying your workflow and enhancing efficiency. With this feature, users can effortlessly preview their documents with a glance, enabling swift navigation and ensuring seamless accessibility to crucial content.

Generate document preview

To create document preview:

  1. Create an instance of the Watermarker class for a local file or file stream;

  2. Instantiate the PreviewOptions object with the delegate for stream creation (see event handler CreatePageStream); If you need to implement custom image preview stream disposing you have to pass additional argument ReleasePageStream to clean up resources.

  3. Specify PreviewFormat - PNG / JPG / BMP, and other properties like PageNumbers for which you want to generate preview. Additionaly for most of documents you can use specific PreviewOptions overloads, like PdfPreviewOptions, SpreadsheetPreviewOptions which allows to configure the resolution for the generated images in dots per inch.

  4. Call method GeneratePreview method of the Watermarker object and pass PreviewOptions to it.

string outputDirectory = Constants.GetOutputDirectoryPath();

using (Watermarker watermarker = new Watermarker("sample.pdf"))
{
    CreatePageStream createPageStreamDelegate = delegate(int number)
    {
        string previewImageFileName = Path.Combine(outputDirectory, 
            string.Format("page{0}.png", number));
        return File.OpenWrite(previewImageFileName);
    };

    ReleasePageStream releasePageStreamDelegate = delegate(int number, Stream stream)
    {
        stream.Close();
    };

    PreviewOptions previewOptions = new PreviewOptions(createPageStreamDelegate,
        releasePageStreamDelegate)
    {
        PreviewFormat = PreviewOptions.PreviewFormats.PNG,
        PageNumbers = new []{1, 2}
    };
    
    watermarker.GeneratePreview(previewOptions);
}

Run the program. It will create images in the outputDirectory corresponding to the pages of the document.