replaceVariablebyHTML

replaceVariablebyHTML

BASIC / ADVANCED / PREMIUM

Replaces a single variable by HTML.

Description
public void replaceVariableByHTML(String var, String html) throws Exception
public void replaceVariableByHTML(String var, String type, String html) throws Exception
public void replaceVariableByHTML(String var, String type, String html, OptionsHTML options) throws Exception

This method allows the insertion of HTML into a template. You may insert it as a "block", e.g. removing the corresponding paragraph and substituting it by the block of WordML, or "inline", e.g. just replacing the variable by inline WordML.

This method transforms HTML directly into WordML and it is compatible with all DOCX readers.

You may find a more detailed explanation of this useful method in the HTML to Word section of the API documentation.

Parameters

var

The template variable to be replaced by the HTML content.

type

The substitution type, it can be:

  • "block": the variable, together with its containing paragraph, is replaced by the HTML content.
  • "inline": only the variable is replaced by the inline HTML content.

html

The HTML code to be translated into WordML: it can be a string or the path to a file.

options

The possible keys and values are:

Key Type Description
addDefaultStyles Boolean True as default, if false prevents adding default styles when strictWordStyles is false.
baseURL String The base URL used to complete the relative paths of links and images.
customListStyles Boolean If true checks if there is a custom list style with that name and uses it.
disableJavaScript Boolean True as default, if false enables JavaScript.
downloadImages Boolean If true inserts the images into the DOCX document, otherwise just links them as an external source.
filter String Only renders the filtered contents using a CSS selector.
isFile Boolean True for files and false for strings.
parseAnchors Boolean If true parses the anchors included in the HTML code.
parseDivs string paragraph. By default the method parses the div elements as containers.
parseFloats Boolean If true preserves the floating properties of images and tables.
strictWordStyles Boolean If true ignores all CSS styles and uses the styles set via the wordStyles option (see next).
target String Possible values are: document (default), header, footer, footnote, endnote, comment. This option sets the scope of the replacement procedure.
wordStyles HashMap<String, String> Associate different Word styles to HTML classes, ids or tags.
Code samples

Example #1

The resulting Word document looks like: