Output Format and Password

The WordProcessingSaveOptions class is responsible for configuring the saving process when an EditableDocument instance—containing the edited document content—is converted into an output document of a specific WordProcessing format. In other words, WordProcessingSaveOptions specify how the edited HTML should be saved to the output document.

Unlike load and edit options, the WordProcessingSaveOptions class requires one mandatory constructor parameter: outputFormat. The outputFormat parameter has a WordProcessingFormats type. This type includes all supported formats from the WordProcessing format family that are available for saving documents. Each field of the WordProcessingFormats class represents a separate WordProcessing format—such as DOC, DOCX, RTF, ODT, etc. There’s also an All field that allows you to enumerate over all WordProcessing formats.

With the outputFormat parameter, you should select the specific format of the output WordProcessing document to be generated by the editor.save() method.

This constructor is mandatory because it’s essential for GroupDocs.Editor to know the specific format in which the document should be saved. However, the output format can be changed later, after creating an instance of the WordProcessingSaveOptions class, using the setOutputFormat() method. You can also retrieve the currently set output format using the getOutputFormat() method.

Almost all WordProcessing formats, especially those with a binary nature, support file encryption with a password. If such a document is encrypted, it requires a password to open. The WordProcessingSaveOptions class has the setPassword() method, which allows you to set a password for the output document:

// Getter and Setter for password
public String getPassword()
public void setPassword(String value)

By default, the value of this property is null, which means that no password will be applied. If you specify a string in this property, the output document will be encrypted and protected with this string as a password. If you have previously set a password but later decide to remove it, you can set the value to null or an empty string—both will be interpreted as “do not set a password” when calling the editor.save() method.

Example Usage

The following example demonstrates how to change the output format and set a password for the edited WordProcessing document using GroupDocs.Editor for Node.js via Java.

// Import necessary modules
const groupdocs_editor = require('groupdocs-editor');
const fs = require('fs');

// Load the source WordProcessing document
const editor = new groupdocs_editor.Editor('SampleDocument.docx');

// Create WordProcessingEditOptions if needed
const editOptions = new groupdocs_editor.WordProcessingEditOptions();

// Edit the document to get an EditableDocument instance
const editableDocument = editor.edit(editOptions);

// Create WordProcessingSaveOptions with the desired output format
// For example, to save the document in RTF format
const saveOptions = new groupdocs_editor.WordProcessingSaveOptions(groupdocs_editor.WordProcessingFormats.Rtf);

// Set a password for the output document
saveOptions.setPassword('yourPassword123');

// Save the edited document with the specified save options
editor.save(editableDocument, 'OutputDocument.rtf', saveOptions);

console.log('Document saved successfully with the specified format and password.');

Notes:

  • Replace 'SampleDocument.docx' with the path to your source WordProcessing document.
  • Replace 'OutputDocument.rtf' with the desired output file path.
  • Replace 'yourPassword123' with your desired password.

Changing Output Formats

You can choose from various WordProcessing formats when saving the document. The WordProcessingFormats class provides constants for all supported formats. Here are some examples:

// Save as DOCX
const saveOptions = new groupdocs_editor.WordProcessingSaveOptions(groupdocs_editor.WordProcessingFormats.Docx);

// Save as DOC
const saveOptions = new groupdocs_editor.WordProcessingSaveOptions(groupdocs_editor.WordProcessingFormats.Doc);

// Save as RTF
const saveOptions = new groupdocs_editor.WordProcessingSaveOptions(groupdocs_editor.WordProcessingFormats.Rtf);

// Save as ODT
const saveOptions = new groupdocs_editor.WordProcessingSaveOptions(groupdocs_editor.WordProcessingFormats.Odt);

You can also change the output format after creating the WordProcessingSaveOptions instance:

// Create save options with initial format
const saveOptions = new groupdocs_editor.WordProcessingSaveOptions(groupdocs_editor.WordProcessingFormats.Docx);

// Later in the code, change the output format to ODT
saveOptions.setOutputFormat(groupdocs_editor.WordProcessingFormats.Odt);

Removing the Password

If you have set a password previously and want to save the document without encryption, set the password to null or an empty string:

// Remove the password protection
saveOptions.setPassword(null);
// or
saveOptions.setPassword('');

Complete Example

Below is a complete example that demonstrates changing the output format to DOC, setting a password, and then removing the password before saving:

// Import necessary modules
const groupdocs_editor = require('groupdocs-editor');
const fs = require('fs');

// Load the source WordProcessing document
const editor = new groupdocs_editor.Editor('SampleDocument.docx');

// Create WordProcessingEditOptions if needed
const editOptions = new groupdocs_editor.WordProcessingEditOptions();

// Edit the document to get an EditableDocument instance
const editableDocument = editor.edit(editOptions);

// Create WordProcessingSaveOptions with the desired output format (DOC)
const saveOptions = new groupdocs_editor.WordProcessingSaveOptions(groupdocs_editor.WordProcessingFormats.Doc);

// Set a password for the output document
saveOptions.setPassword('securePass!');

// Later, decide to remove the password
saveOptions.setPassword(null); // or saveOptions.setPassword('');

// Save the edited document with the specified save options
editor.save(editableDocument, 'OutputDocument.doc', saveOptions);

console.log('Document saved successfully without password protection.');

Conclusion

The WordProcessingSaveOptions class in GroupDocs.Editor for Node.js via Java provides flexible options for configuring the saving process of edited WordProcessing documents. You can specify the output format and set or remove passwords for the output document, giving you control over the format and security of the files you generate.

For more examples and detailed information, you can refer to the GroupDocs.Editor for Node.js via Java Examples repository on GitHub.