This article describes how to load an input document into GroupDocs.Editor for Node.js via Java and how to apply load options.
First, the input document, which should be accessible as a byte stream or through a valid file path, should be loaded into GroupDocs.Editor by creating an instance of the Editor class through one of its constructor overloads. The source code below shows two ways of loading documents: from a path and from a stream.
// Import the necessary modules
constgroupdocsEditor=require('@groupdocs/groupdocs.editor');constfs=require('fs');// Loading from path
constinputFilePath='C:\\input_path\\document.docx';// Path to some document
consteditor=newgroupdocsEditor.Editor(inputFilePath);// Loading from stream
constinputStream=fs.createReadStream(inputFilePath);consteditor=newgroupdocsEditor.Editor(inputStream);
When using the two overloads from the example above, GroupDocs.Editor automatically detects the format of the input document and applies the most appropriate default loading options for it.
However, it is possible and even recommended to specify correct loading options explicitly using constructor overloads that accept two parameters. The source code below shows how to use such options.
// Import the necessary modules
constgroupdocsEditor=require('@groupdocs/groupdocs.editor');constfs=require('fs');// Loading from path with load options
constinputFilePath='C:\\input_path\\document.docx';// Path to some document
constwordLoadOptions=newgroupdocsEditor.WordProcessingLoadOptions();consteditor1=newgroupdocsEditor.Editor(inputFilePath,wordLoadOptions);// Loading from stream with load options
constinputStream=fs.createReadStream(inputFilePath);constspreadsheetLoadOptions=newgroupdocsEditor.SpreadsheetLoadOptions();consteditor=newgroupdocsEditor.Editor(inputStream,spreadsheetLoadOptions);
Please note that not all document formats have appropriate classes that represent load options. As of version 23.10, only WordProcessing, Spreadsheet, Presentation, and some other family formats have load options. For other document types, such as DSV, TXT, or XML, there are no load options.
Format Family
Example Formats
Load Options Class
WordProcessing
DOC, DOCX, DOCM, DOT, ODT
WordProcessingLoadOptions
Spreadsheet
XLS, XLSX, XLSM, XLSB
SpreadsheetLoadOptions
Presentation
PPT, PPTX, PPS, POT
PresentationLoadOptions
Email
EML, MSG, EMLX, MHT
EmailLoadOptions
PDF
PDF
PdfLoadOptions
Using load options is essential when working with password-protected input documents. Any document can be loaded into the Editor instance, even an encrypted document without the password. However, in the next step—opening for editing—an exception will be thrown. GroupDocs.Editor handles passwords and encrypted documents in the following way:
Document Not Encrypted:
If the document is not encrypted, the password is ignored, whether or not it was specified.
Document Encrypted, Password Not Specified:
If the document is password-protected but no password is specified, a PasswordRequiredException will be thrown later during editing.
Incorrect Password Specified:
If the document is password-protected and a password is specified but is incorrect, an IncorrectPasswordException will be thrown later during editing.
The example below shows how to specify a password for opening a password-protected WordProcessing document.
// Import the necessary modules
constgroupdocsEditor=require('@groupdocs/groupdocs.editor');constfs=require('fs');// Loading a password-protected document
constinputFilePath='C:\\input_path\\protected_document.docx';constinputStream=fs.createReadStream(inputFilePath);constwordLoadOptions=newgroupdocsEditor.WordProcessingLoadOptions();wordLoadOptions.setPassword('correct_password');consteditor=newgroupdocsEditor.Editor(inputStream,wordLoadOptions);
Note: The same approach is applicable for Spreadsheet and Presentation documents as well.
Tip: Always handle exceptions appropriately in your applications, especially when dealing with password-protected or unsupported documents.
Conclusion
This article has demonstrated how to load documents into GroupDocs.Editor for Node.js via Java using different methods and how to apply load options, especially when dealing with password-protected documents. By specifying the appropriate load options, you ensure that your documents are correctly processed and ready for editing.