Set password for output document

On this page

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

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

'use strict';

// Import the GroupDocs.Comparison for Node.js via Java SDK
const groupdocs = require('@groupdocs/groupdocs.comparison');

// Define file paths for the source, target, and password-protected result
const sourceFile = 'sample-files/source.docx';
const targetFile = 'sample-files/target.docx';
const outputFile = 'result.docx';

// Initialize the comparer with the source document
const comparer = new groupdocs.Comparer(sourceFile);

// Add the target document to the comparison session
comparer.add(targetFile);

// Configure comparison options to indicate that a user password will be set
const compareOptions = new groupdocs.CompareOptions();
compareOptions.setPasswordSaveOption(groupdocs.PasswordSaveOption.USER);

// Configure save options with the desired document password
const saveOptions = new groupdocs.SaveOptions();
saveOptions.setPassword('3333'); // password that will be required to open the result

// Run the comparison, applying both save and comparison options
comparer.compare(outputFile, saveOptions, compareOptions);

// Terminate the process with a success exit code
process.exit(0);

This example creates a Comparer instance with the source document, adds the target document, and creates both CompareOptions and SaveOptions objects. It sets setPasswordSaveOption(PasswordSaveOption.USER) in the CompareOptions to indicate that a user password will be applied, and sets the actual password using setPassword('3333') in the SaveOptions. When the comparison is performed with both options, the result document will be password-protected and require the specified password to open.

On this page

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.