Get document page 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.Redaction 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-redaction -- dnx GroupDocs.Redaction.Mcp --yes
Folders, licensing and more →

Redactor renders document pages to images in one of these formats:

  • Portable Network Graphics (PNG)
  • JPEG Image
  • Bitmap Image File (BMP)

Pass a PreviewOptions to generate_preview(). Its first argument is a function that receives a page number and returns a writable binary stream for that page: an open file or an io.BytesIO. Set width, height and page_numbers: the engine raises an error when any of them is left unset.

Save a page preview to a file

The following example renders the first page of a document to a PNG file:

from groupdocs.redaction import Redactor
from groupdocs.redaction.options import PreviewOptions


def get_document_page_preview():
    # Return a writable stream for each page the engine renders
    def create_page_stream(page_number):
        return open("preview_page_%d.png" % page_number, "wb")

    with Redactor("./multipage_sample.docx") as redactor:
        options = PreviewOptions(create_page_stream)
        options.width = 480
        options.height = 640
        options.page_numbers = [1]
        options.preview_format = PreviewOptions.PreviewFormats.PNG
        redactor.generate_preview(options)
        print("The preview of page 1 is saved to preview_page_1.png")


if __name__ == "__main__":
    get_document_page_preview()

multipage_sample.docx is the sample file used in this example. Click here to download it.

Binary file (PNG, 27 KB)

Download full output

The engine closes each stream once it has written the page, so the file is complete when generate_preview() returns.

Render page previews into memory

A second function, passed after the first, is called when a page is done. It receives the page number and the stream the first function returned for it. With an io.BytesIO, the page’s image is in the buffer by then:

import io

from groupdocs.redaction import Redactor
from groupdocs.redaction.options import PreviewOptions


def get_page_previews_in_memory():
    def create_page_stream(page_number):
        return io.BytesIO()

    def release_page_stream(page_number, stream):
        # The stream is closed and filled: keep, upload or inspect the image
        print("Page %d: %d bytes of JPEG" % (page_number, len(stream.getvalue())))

    with Redactor("./multipage_sample.docx") as redactor:
        options = PreviewOptions(create_page_stream, release_page_stream)
        options.width = 300
        options.height = 400
        options.page_numbers = [1, 2, 3]
        options.preview_format = PreviewOptions.PreviewFormats.JPEG
        redactor.generate_preview(options)


if __name__ == "__main__":
    get_page_previews_in_memory()

multipage_sample.docx is the sample file used in this example. Click here to download it.

Page 1: 14013 bytes of JPEG
Page 2: 13575 bytes of JPEG
Page 3: 11155 bytes of JPEG

Download full output

A preview shows the document as it was loaded, without the redactions applied since. To preview the redacted result, save it and open the saved document with a new Redactor.