Set document metadata on save

Document can contain some metadata information, such as author, organization, etc. GroupDocs.Comparison allows you to select metadata source when saving 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. Call the setCloneMetadataType() method to set the appropriate MetadataType variant.
  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 source file

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    final SaveOptions saveOptions = new SaveOptions();
    saveOptions.setCloneMetadataType(MetadataType.SOURCE);
    final Path resultPath = comparer.compare(outputFile, saveOptions);
}

Set metadata from target file

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    final SaveOptions saveOptions = new SaveOptions();
    saveOptions.setCloneMetadataType(MetadataType.TARGET);
    final Path resultPath = comparer.compare(outputFile, saveOptions);
}

Set user-defined metadata 


try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    final FileAuthorMetadata fileAuthorMetadata = new FileAuthorMetadata();
    fileAuthorMetadata.setAuthor("Tom");
    fileAuthorMetadata.setCompany("GroupDocs");
    fileAuthorMetadata.setLastSaveBy("Jack");
    final SaveOptions saveOptions = new SaveOptions();
    saveOptions.setCloneMetadataType(MetadataType.FILEAUTHOR);
    saveOptions.setFileAuthorMetadata(fileAuthorMetadata);
    final Path resultPath = comparer.compare(outputFile, saveOptions);
}