Set password for output document

On this page

GroupDocs.Comparison allows you to protect the output document with a password.

To protect the output document, follow these steps:

  1. Instantiate the Comparer object. Specify the source document path or stream.
  2. Call the add() method. Specify the target document path or stream.
  3. Instantiate the CompareOptions object. Call the password_save_option method and specify the PasswordSaveOption.USER value.
  4. Instantiate the SaveOptions object. Call the password property to specify a password string.
  5. Call the compare() method. Specify the SaveOptions and CompareOptions objects as parameters.

The following code snippet shows how to compare documents and protect the output document with a password:

import groupdocs.comparison as gc

def set_password_for_resultant_document(output_file_path, source_file_path, target_file_path):
    try:
        # Initialize comparer with the source document
        with gc.Comparer(source_file_path) as comparer:
            # Add the target document for comparison
            comparer.add(target_file_path)
            
            # Set comparison options
            compare_options = gc.options.CompareOptions()
            compare_options.password_save_option = gc.options.PasswordSaveOption.USER
            
            # Set save options
            save_options = gc.options.SaveOptions()
            save_options.password = "3333"
            
            # Compare the documents and save the result
            comparer.compare(output_file_path, save_options, compare_options)
        
        # Log the success message with the output file path
        print(f"\nDocuments compared successfully.\nCheck output in {output_file_path}.")
    
    except Exception as error:
        # Error handling
        print(f'An error occurred during the document comparison: {error}')

On this page