Generating Document Previews

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.Assembly 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-assembly -- dnx GroupDocs.Assembly.Mcp --yes
Folders, licensing and more →
Note
This feature is supported by version 26.11 or greater.

DocumentAssembler.GeneratePreview renders pages of a document to images. The document can be a template, a document assembled by GroupDocs.Assembly, or any other document of a supported format: word processing, spreadsheet, presentation, email or text. PreviewOptions follows the preview options of other GroupDocs products.

The document is passed by a path or a stream:

  • void GeneratePreview(string documentPath, PreviewOptions previewOptions)
  • void GeneratePreview(Stream documentStream, PreviewOptions previewOptions)

Preview Options

MemberDescription
CreatePageStreamA delegate called for every page to get the stream the page image is written to. Passed to the constructor.
ReleasePageStreamAn optional delegate called when a page image has been written, to close or process its stream. Passed to the constructor.
PreviewFormatThe image format: PreviewFormats.PNG (the default), PreviewFormats.JPEG or PreviewFormats.BMP.
PageNumbersThe numbers of the pages to render, starting from 1. All pages by default.
Width, HeightThe size of page images in pixels. When both are set, pages are scaled to fit that size keeping their aspect ratio, and Resolution is ignored.
ResolutionThe resolution of page images in dots per inch, 96 by default.

Rendering Pages of an Assembled Report

The example assembles a report into memory and renders its pages to PNG files:

using System.IO;
using GroupDocs.Assembly;
using GroupDocs.Assembly.Data;

DocumentAssembler assembler = new DocumentAssembler();

using (FileStream template = File.OpenRead("Customer orders.docx"))
using (MemoryStream report = new MemoryStream())
{
    assembler.AssembleDocument(template, report,
        new DataSourceInfo(new JsonDataSource("Customers.json"), "customers"));
    report.Position = 0;

    // A stream is created for every page and released when the page is written.
    PreviewOptions options = new PreviewOptions(
        pageNumber => File.Create($"page{pageNumber}.png"),
        (pageNumber, pageStream) => pageStream.Dispose());
    options.PreviewFormat = PreviewFormats.PNG;
    options.Resolution = 120;               // dots per inch; 96 by default
    // options.PageNumbers = new[] { 1, 2 }; // all pages by default

    assembler.GeneratePreview(report, options);
}

Rendering Selected Pages of a Template

To preview a template itself, pass its path. Here the first page is rendered to a JPEG image that fits in 400 × 400 pixels:

PreviewOptions options = new PreviewOptions(pageNumber => File.Create($"template-page{pageNumber}.jpg"),
    (pageNumber, pageStream) => pageStream.Dispose());
options.PreviewFormat = PreviewFormats.JPEG;
options.PageNumbers = new[] { 1 };
options.Width = 400;
options.Height = 400;

new DocumentAssembler().GeneratePreview("Customer orders.docx", options);
Note
Previews are generated without loading any external resources.

Example: GenerateDocumentPreview.cs.