In some cases you might need to redact the pages of a document as images, redacting entire areas of the page instead or in addition to a specific text. With GroupDocs.Redaction you can use the following approach:
open the document and apply all required redactions to the document’s body (text, annotations, etc.);
save it as a rasterized PDF file (containing images of the original document’s pages);
apply ImageAreaRedaction to remove specific areas on the pages within the PDF document.
The following example demonstrates how to create a rasterized PDF from a Microsoft Word document and apply image redactions to its pages:
fromioimportBytesIOfromgroupdocs.redactionimportRedactor,RedactionStatusfromgroupdocs.redaction.optionsimportRasterizationOptionsfromgroupdocs.redaction.redactionsimportImageAreaRedaction,RegionReplacementOptionsfromgroupdocs.pydrawingimportPoint,Size,Colordefcreate_pdf_with_image_redaction():# Create an in-memory binary streamstream=BytesIO()# Load the document and rasterize it to a PDF in the streamwithRedactor("./sample.docx")asredactor:# Save the rasterized document to the streamro=RasterizationOptions()ro.enabled=Trueredactor.save(stream,ro)stream.seek(0)# Define the position on the imagesample_point=Point(40,160)# Define the size of the area which needs to be redactedsample_size=Size(350,75)# Define the color of redactioncolor=Color.from_argb(255,220,20,60)# Specify the redaction optionsrepl_opt=RegionReplacementOptions(color,sample_size)img_red=ImageAreaRedaction(sample_point,repl_opt)# Load the stream to be redactedwithRedactor(stream)asredactor:# Apply the redactionresult=redactor.apply(img_red)# Re-open the rasterized PDF document to redact its pages as imagesifresult.status!=RedactionStatus.FAILED:# Save the document to a file and convert its pages to imagesro=RasterizationOptions()ro.enabled=Truewithopen("./redacted-sample.pdf","wb")asstream_out:redactor.save(stream_out,ro)print("Document redacted successfully.\nCheck output in ./redacted-sample.pdf")else:print("Redaction failed!")if__name__=="__main__":create_pdf_with_image_redaction()
sample.docx is the sample file used in this example. Click here to download it.
Please, note that you don’t have to use GroupDocs.Redaction to create a rasterized PDF from an office document. You will be able to use it, if you don’t have any other tool for that.
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.