Protect PDF document

You can protect a PDF document by setting the following parameters:

  • permissions
  • password to open document
  • password to change permissions

To protect a PDF document, follow these steps:

  1. Create an instance of the Viewer class. Specify the source document path as a constructor parameter.
  2. Instantiate the Security object.
  3. To specify a password to open the document, set the DocumentOpenPassword property.
  4. To specify a password to change restrictions, set the PermissionsPassword property.
  5. To specify the document permissions, set the Permissions property.
  6. Instantiate the PdfViewOptions object. Specify the saving path format for the rendered document.
  7. Initialize the Security property of the PdfViewOptions object with the object created in steps 2-5.
  8. Call the view method of the Viewer object. Specify the PdfViewOptions object as the parameter.

The following code snippet shows how to protect the output PDF document:

with gv.Viewer("sample.docx") as viewer:
    # Specify the security settings.
    security = gvo.Security()
    security.document_open_password = "o123"
    security.permissions_password = "p123"
    security.permissions = gvo.Permissions.ALLOW_ALL & ~gvo.Permissions.DENY_PRINTING

    # Create a PDF file.
    pdf_options = gvo.PdfViewOptions()

    # Apply the security settings
    pdf_options.security = security
    viewer.view(pdf_options)