Set document metadata on save

A document can contain some metadata information, such as author, organization, etc. GroupDocs.Comparison allows you to select metadata sources when saving the output document.

Possible metadata sources are as follows:

  • Source document metadata
  • Target document metadata
  • User-defined metadata

To set output document metadata, 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 SaveOptions object. Set the appropriate MetadataType via clone_metadata_type property.
  4. Call the compare() method. Specify the SaveOptions object as a parameter.

The following code snippets show how to set output document metadata:

Set metadata from the source file

import groupdocs.comparison as gc

def set_document_metadata_on_save(output_file_path, source_file_path, target_file_path):
    # 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
        save_options = gc.options.SaveOptions()
        save_options.clone_metadata_type = gc.options.MetadataType.SOURCE
        
        # Compare the documents and save the result
        comparer.compare(output_file_path, save_options)
    
    # Log the success message with the output file path
    print(f"\nDocuments compared successfully.\nCheck output in {output_file_path}.")

Set metadata from the target file

import groupdocs.comparison as gc

def set_document_metadata_on_save(output_file_path, source_file_path, target_file_path):
    # 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
        save_options = gc.options.SaveOptions()
        save_options.clone_metadata_type = gc.options.MetadataType.TARGET
        
        # Compare the documents and save the result
        comparer.compare(output_file_path, save_options)
    
    # Log the success message with the output file path
    print(f"\nDocuments compared successfully.\nCheck output in {output_file_path}.")

Set user-defined metadata 

import groupdocs.comparison as gc

def set_document_metadata_on_save(output_file_path, source_file_path, target_file_path):
    # 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 file author metadata
        file_author_metadata = gc.options.FileAuthorMetadata()
        file_author_metadata.author = "Tom"
        file_author_metadata.company = "GroupDocs"
        file_author_metadata.last_save_by = "Jack"

        # Set comparison options
        save_options = gc.options.SaveOptions()
        save_options.clone_metadata_type = gc.options.MetadataType.FILE_AUTHOR
        save_options.file_author_metadata = file_author_metadata
        
        # Compare the documents and save the result
        comparer.compare(output_file_path, save_options)
    
    # Log the success message with the output file path
    print(f"\nDocuments compared successfully.\nCheck output in {output_file_path}.")