Working with resources

This demonstration shows and explains different operations with resources, including retrieving, adjusting and saving them in different scenarios.

Introduction

Almost all documents of any types have resources. It is first of all images, some document formats also hold fonts. Even for plain text document (TXT), when converting it to the HTML for editing, there will be one stylesheet, that is treated as a resource. GroupDocs.Editor allows to work with resources on editing phase, when document was loaded to the Editor class and opened for editing by generating the EditableDocument instance, that is produced by the Editor.Edit method. Instance of EditableDocumentcan be treated as an input document, converted to 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 onto three groups:

  • Images, including: raster (PNG, BMP, JPEG, GIF, ICON) and vector (SVG and WMF).
  • Fonts, including: TTF, EOT, WOFF, WOFF2.
  • Textual resources: only CSS.

Every resource type has its own distinct class with metadata, constructors and other methods. They all are located within GroupDocs.Editor.HtmlCss.Resources namespace.

Preparations

Let’s prepare an EditableDocument instance by loading and editing some input WordProcessing document, as always (using directive is omitted for the sake of simplification):

String inputDocxPath = "C://input/document.docx";
Editor editor = new Editor(inputDocxPath, new WordProcessingLoadOptions());
WordProcessingEditOptions editOptions = new WordProcessingEditOptions();
editOptions.setFontExtraction(FontExtractionOptions.ExtractAll);// Enable max font extraction - ExtractAll
EditableDocument beforeEdit = editor.edit(editOptions);// Create EditableDocument instance

Obtaining resources

Now, when EditableDocument instance is ready, it is possible to obtains resources from it, and EditableDocument provides several ways for this.

First of all, resources can be retrieved by their type. EditableDocument contains three properties for every resource type:

Secondly, completely all resources may be obtained with a single property EditableDocument.AllResources. It returns a List of IHtmlResource, interface, that is common for absolutely all HTML resources, including images, fonts and stylesheets. The collection, returned by the AllResources property, in fact is a concatenation of previous three.

Here is a source code:

List<IImageResource> images = beforeEdit.getImages();
List<FontResourceBase> fonts = beforeEdit.getFonts();
List<CssText> stylesheets = beforeEdit.getCss();
List<IHtmlResource> allTogether = beforeEdit.getAllResources();

If user wants to manually save instance of EditableDocument as HTML file with resources, he may use something like this:

String outputFolder = "C://output//document_resources//";
for (IImageResource oneImage : (Iterable<IImageResource>) images) {
	oneImage.save(outputFolder + oneImage.getFilenameWithExtension());
}

for (FontResourceBase oneFont : (Iterable<FontResourceBase>) fonts) {
	oneFont.save(outputFolder + oneFont.getFilenameWithExtension());
}

for (CssText oneStylesheet : (Iterable<CssText>) stylesheets) {
	oneStylesheet.save(outputFolder + oneStylesheet.getFilenameWithExtension());
}

try (PrintWriter out = new PrintWriter("c://output//document.html")) {
	out.println(beforeEdit.getContent());
}

CSS resources

There also is a third way, that is designed especially for the stylesheets. The reason is that stylesheets can contain external resources too, which are presented as links with urls. For example, it can be images, fonts, and other stylesheets. In such case it is necessary to adjust such link. For coping with this EditableDocument contains two overloads of the GetCssContent() method. First overload is parameterless and returns a List of strings, each one represents one stylesheet. In most cases document has only one stylesheet, so such list will have only one item. Second overload obtains two string parameters: externalImagesPrefix and externalFontsPrefix. First parameter is designated for images, while second - for fonts. Both overloads are shown in example below:

List<String> stylesheetsWithoutPrefixes = beforeEdit.getCssContent();
String externalImagesPrefix = "http://www.mywebsite.com/images/id=";
String externalFontsPrefix = "http://www.mywebsite.com/fonts/id=";
List<String> stylesheetsWithPrefixes = beforeEdit.getCssContent(externalImagesPrefix, externalFontsPrefix);

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple but powerful free Apps.
You are welcome to edit your Microsoft Word (DOC, DOCX, RTF etc.), Microsoft Excel (XLS, XLSX, CSV etc.), Open Document (ODT, OTT, ODS) and other documents with free to use online GroupDocs Editor App.