This guide demonstrates and explains different operations with resources, including retrieving, adjusting, and saving them in different scenarios using GroupDocs.Editor for Node.js via Java.
Introduction
Almost all documents of any type have resources. First of all, images; some document formats also hold fonts. Even for plain text documents (TXT), when converting them to HTML for editing, there will be one stylesheet, which is treated as a resource. GroupDocs.Editor allows you to work with resources during the editing phase when a document is loaded into the Editor class and opened for editing by generating the EditableDocument instance, which is produced by the editor.edit() method. An instance of EditableDocument can be treated as an input document converted to an internal intermediate format, with possibilities to generate HTML markup and resources for passing them to the client-side WYSIWYG HTML editor. GroupDocs.Editor classifies all resources into four groups:
Images, including raster (PNG, BMP, JPEG, GIF, ICON) and vector (SVG and WMF).
Fonts, including TTF, EOT, WOFF, WOFF2.
Textual resources: only CSS.
Audio files: only MP3 (appeared in version 22.6).
Every resource type has its own distinct class with metadata, constructors, and other methods. They all are accessible through the groupdocs-editor module.
Preparations
Let’s prepare an EditableDocument instance by loading and editing an input WordProcessing document. The following code demonstrates how to achieve this:
// Import necessary modules
constgroupdocsEditor=require('groupdocs-editor');// Specify the input DOCX file path
constinputDocxPath='C://input/document.docx';// Create an instance of Editor with optional load options
consteditor=newgroupdocsEditor.Editor(inputDocxPath,newgroupdocsEditor.WordProcessingLoadOptions());// Prepare edit options
consteditOptions=newgroupdocsEditor.WordProcessingEditOptions();// Enable maximum font extraction
editOptions.setFontExtraction(groupdocsEditor.FontExtractionOptions.ExtractAll);// Create EditableDocument instance
constbeforeEdit=editor.edit(editOptions);
In this example:
We import the groupdocs-editor module.
Specify the path to the input WordProcessing document.
Create an instance of the Editor class.
Prepare WordProcessingEditOptions and set font extraction to extract all fonts.
Call the editor.edit() method to create an EditableDocument instance.
Obtaining Resources
Now that the EditableDocument instance is ready, it is possible to obtain resources from it. The EditableDocument provides several ways to do this.
First of all, resources can be retrieved by their type. The EditableDocument contains methods for each resource type:
getImages(): Returns an array of IImageResource instances, which is common for all images, raster and vector.
getFonts(): Returns an array of FontResourceBase instances, which is common for all fonts.
getCss(): Returns an array of CssText instances, representing CSS stylesheets.
getAudio(): Returns an array of Mp3Audio instances, representing MP3 audio files (added in version 21.10).
Secondly, all resources can be obtained with a single method getAllResources(). It returns an array of IHtmlResource instances, which is common for all HTML resources, including images, fonts, stylesheets, and audio files. The collection returned by the getAllResources() method is essentially a concatenation of the previous resource arrays.
Here’s the source code:
// Get images
constimages=beforeEdit.getImages();// Get fonts
constfonts=beforeEdit.getFonts();// Get stylesheets
conststylesheets=beforeEdit.getCss();// Get audio resources (if any)
constaudioFiles=beforeEdit.getAudio();// Get all resources together
constallTogether=beforeEdit.getAllResources();
If you want to manually save the instance of EditableDocument as an HTML file with resources, you may use code like this:
// Import necessary modules
constfs=require('fs');constpath=require('path');// Define the output folder
constoutputFolder='C://output//document_resources//';// Ensure the output folder exists
if(!fs.existsSync(outputFolder)){fs.mkdirSync(outputFolder,{recursive:true});}// Save images
images.forEach((oneImage)=>{constimagePath=path.join(outputFolder,oneImage.getFilenameWithExtension());fs.writeFileSync(imagePath,oneImage.getByteContent());});// Save fonts
fonts.forEach((oneFont)=>{constfontPath=path.join(outputFolder,oneFont.getFilenameWithExtension());fs.writeFileSync(fontPath,oneFont.getByteContent());});// Save stylesheets
stylesheets.forEach((oneStylesheet)=>{conststylesheetPath=path.join(outputFolder,oneStylesheet.getFilenameWithExtension());fs.writeFileSync(stylesheetPath,oneStylesheet.getTextContent());});// Save audio files (if any)
audioFiles.forEach((oneAudio)=>{constaudioPath=path.join(outputFolder,oneAudio.getFilenameWithExtension());fs.writeFileSync(audioPath,oneAudio.getByteContent());});// Save the HTML content
fs.writeFileSync('C://output//document.html',beforeEdit.getContent());
In this example:
We import the fs and path modules to handle file system operations.
Define the output folder where resources will be saved.
Ensure the output folder exists, creating it if necessary.
Iterate over each resource type (images, fonts, stylesheets, audio) and save each resource to the output folder.
Save the HTML content to a file.
CSS Resources
There is also a way designed especially for stylesheets. Stylesheets can contain external resources too, which are presented as links with URLs. For example, they can reference images, fonts, and other stylesheets. In such cases, it is necessary to adjust such links. To cope with this, the EditableDocument contains two overloads of the getCssContent() method.
First overload: Parameterless, returns an array of strings, each representing one stylesheet. In most cases, the document has only one stylesheet, so the array will have only one item.
Second overload: Accepts two string parameters: externalImagesPrefix and externalFontsPrefix. The first parameter is designated for images, while the second is for fonts.
Both overloads are shown in the example below:
// Get stylesheets without prefixes
conststylesheetsWithoutPrefixes=beforeEdit.getCssContent();// Define external prefixes
constexternalImagesPrefix="http://www.mywebsite.com/images/id=";constexternalFontsPrefix="http://www.mywebsite.com/fonts/id=";// Get stylesheets with prefixes
conststylesheetsWithPrefixes=beforeEdit.getCssContent(externalImagesPrefix,externalFontsPrefix);
In this example:
We first retrieve the stylesheets without any external resource prefixes.
Then, we define external prefixes for images and fonts.
Use the getCssContent() method with the prefixes to adjust the resource links within the stylesheets.
Conclusion
By following this guide, you can effectively work with resources when editing documents using GroupDocs.Editor for Node.js via Java. You can retrieve, adjust, and save images, fonts, stylesheets, and audio files associated with your documents. This enables you to have full control over the resources when integrating with client-side WYSIWYG HTML editors or for other purposes.
Note: Be sure to replace the paths in the code examples with the actual paths used in your application.