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:
fromgroupdocs.redactionimportRedactorfromgroupdocs.redaction.optionsimportPreviewOptionsdefget_document_page_preview():# Return a writable stream for each page the engine rendersdefcreate_page_stream(page_number):returnopen("preview_page_%d.png"%page_number,"wb")withRedactor("./multipage_sample.docx")asredactor:options=PreviewOptions(create_page_stream)options.width=480options.height=640options.page_numbers=[1]options.preview_format=PreviewOptions.PreviewFormats.PNGredactor.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.
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:
importiofromgroupdocs.redactionimportRedactorfromgroupdocs.redaction.optionsimportPreviewOptionsdefget_page_previews_in_memory():defcreate_page_stream(page_number):returnio.BytesIO()defrelease_page_stream(page_number,stream):# The stream is closed and filled: keep, upload or inspect the imageprint("Page %d: %d bytes of JPEG"%(page_number,len(stream.getvalue())))withRedactor("./multipage_sample.docx")asredactor:options=PreviewOptions(create_page_stream,release_page_stream)options.width=300options.height=400options.page_numbers=[1,2,3]options.preview_format=PreviewOptions.PreviewFormats.JPEGredactor.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
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.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.