GroupDocs.Signature provides PreviewOptions class to specify different options to manage document pages preview generation process. The feature also supports archives previewing.
Here are the steps to generate document preview with GroupDocs.Signature:
Create new instance of Signature class and pass source document path or stream as a constructor parameter.
a function that creates a stream for each page image (see CreateDocPageStream);
image preview format - PNG / JPEG / BMP,
page numbers to process;
custom resolution of preview images (if needed).
The stream returned by the create function is closed automatically as soon as the page image is written: a file is closed, and an io.BytesIO already holds the complete image. If you need to process each finished image or clean up resources yourself, pass a release function as an additional argument (see ReleaseDocPageStream).
GroupDocs.Signature calls the create function once for each page and writes the page image into the stream it returns: a writable file object, an io.BytesIO, or a .NET stream. The page_data argument is a PreviewPageData object with the page_number, the file_name of the document and the preview_format.
Page numbers are 0-based: page_data.page_number is 0 for the first page of the document.
defcreate_page_stream(page_data):# 0-based: preview_page_0.png is the first pagereturnopen(f"preview_page_{page_data.page_number}.png","wb")
When you preview an archive, the function is called for the pages of every document in it. Page numbers start at 0 again for each document, so use page_data.file_name as well when you name the images.
ReleasePageStream delegate implementation
The release function receives the same page_data and the same object the create function returned. By the time it is called, the image is complete.
defrelease_page_stream(page_data,page_stream):# page_stream is the object create_page_stream returnedprint(f"Image file {page_stream.name} is ready for preview")
Generate document preview from file on local disk
fromgroupdocs.signatureimportSignaturefromgroupdocs.signature.optionsimportPreviewOptionsdefcreate_page_stream(page_data):# Page numbers are 0-based: preview_page_0.png is the first pageimage_name=f"preview_page_{page_data.page_number}.png"print(f"Saving page {page_data.page_number+1} to {image_name}")returnopen(image_name,"wb")defgenerate_document_preview():withSignature("sample.pdf")assignature:# Create preview options object: one PNG image per pagepreview_options=PreviewOptions(create_page_stream)preview_options.preview_format=PreviewOptions.PreviewFormats.PNG# Generate previewsignature.generate_preview(preview_options)if__name__=="__main__":generate_document_preview()
sample.pdf is the sample file used in this example. Click here to download it.
Generate document preview from stream with custom stream releasing delegate
This example reads the document from a stream and keeps the page images in memory: the create function returns an io.BytesIO, and the release function receives it filled with the complete image.
importiofromgroupdocs.signatureimportSignaturefromgroupdocs.signature.optionsimportPreviewOptionsdefcreate_page_stream(page_data):# Keep each page image in memory instead of writing a filereturnio.BytesIO()defrelease_page_stream(page_data,page_stream):# page_stream is the BytesIO returned above, already holding the whole imageimage=page_stream.getvalue()print(f"Page {page_data.page_number}: {len(image)} bytes of JPEG")defgenerate_document_preview_from_stream():withopen("sample.pdf","rb")asstream:withSignature(stream)assignature:# Create preview options object with both functionspreview_options=PreviewOptions(create_page_stream,release_page_stream)preview_options.preview_format=PreviewOptions.PreviewFormats.JPEG# Generate previewsignature.generate_preview(preview_options)if__name__=="__main__":generate_document_preview_from_stream()
sample.pdf is the sample file used in this example. Click here to download it.
Page 0: 276044 bytes of JPEG
Page 1: 224194 bytes of JPEG
Set page_numbers to preview only some pages. The numbers are 0-based, so [1] selects the second page.
fromgroupdocs.signatureimportSignaturefromgroupdocs.signature.optionsimportPreviewOptionsdefcreate_page_stream(page_data):returnopen(f"preview_page_{page_data.page_number}.png","wb")defrelease_page_stream(page_data,page_stream):print(f"Image file {page_stream.name} is ready for preview")defgenerate_document_preview_of_selected_pages():withSignature("sample.pdf")assignature:preview_options=PreviewOptions(create_page_stream,release_page_stream)# Page numbers are 0-based: [1] is the second pagepreview_options.page_numbers=[1]signature.generate_preview(preview_options)if__name__=="__main__":generate_document_preview_of_selected_pages()
sample.pdf is the sample file used in this example. Click here to download it.
Creating a document preview with custom Resolution
The default resolution is 96 DPI. Pass the resolution as the third argument of PreviewOptions, or set the resolution property.
fromgroupdocs.signatureimportSignaturefromgroupdocs.signature.optionsimportPreviewOptionsdefcreate_page_stream(page_data):returnopen(f"preview_page_{page_data.page_number}_150dpi.jpg","wb")defrelease_page_stream(page_data,page_stream):print(f"Image file {page_stream.name} is ready for preview")defgenerate_document_preview_with_resolution():withSignature("sample.pdf")assignature:resolution=150# Create preview options object: 150 DPI instead of the default 96preview_options=PreviewOptions(create_page_stream,release_page_stream,resolution)preview_options.preview_format=PreviewOptions.PreviewFormats.JPEG# Generate previewsignature.generate_preview(preview_options)if__name__=="__main__":generate_document_preview_with_resolution()
sample.pdf is the sample file used in this example. Click here to download it.