Set password for output document

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Comparison MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-comparison -- dnx GroupDocs.Comparison.Mcp --yes
Folders, licensing and more →

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