DOCXPath
DOCXPath
DOCXPath is a feature of Javadocx available in the Advanced and Premium licenses. Its functionality is to facilitate the low level work with DOCX documents through easy but powerful methods that allow you to insert, delete, replace, clone, move and return almost any Word content.
This feature benefits from the flexibility that a set of options adds. This set allows to filter by content type, position, text, parents, attributes or dynamic queries.
For example, it is possible to add a new image after any paragraph that contains a certain text, to place several new elements before a specific graph, to delete one or more rows from a table or a whole paragraph, or to replace one content with another. The possibilities of DOCXPath are almost endless.
DOCXPath is compatible with new documents and templates, and its working environment is limited to the document content. In the current stable package, headers, footers, footnotes or endnotes are out of its scope.
This method inserts WordFragments after the element specified in the document.
This method deletes existing elements in the document.
This method replaces an existing element by a WordFragment.
This method clones existing elements in the document.
This method moves existing elements in the document.
This method returns the text values of existing elements.
The previous commands act over the first result of a XPath query. If you need to obtain information about the data the Xpath query returns, you can run the getDocxPathQueryInfo method. Thanks to this new method it is possible to get the number of the concurrent results and the string of the query. For example, the following code gets the length of a DOCXPath query (existing paragraphs in the document).
- When inserting elements, you can use four placing types: before, after, inlineBefore and inlineAfter. The first two place the content as a block type (for example, a paragraph). The other two place contents as inline ones.
- The following options indicate the references to the contents: type (String, element type, any as default), contains (String, text value), occurrence (String, exact occurrence or range of contents (e.g.: 2..9, 2.., ..9) or first() or last(), if empty iterate all elements), attributes (HashMap, contains a specific attribute key and value), parent (String, main document body as default, allows to set any parent or a specific one), customQuery (custom XPath query, overwrites any other reference).
- If the option occurrence is not set all references of the query will be changed.
- When setting a referenceNode it is mandatory to employ at least one of the available options. You can combine them freely.
- If the reference you indicate doesn't exist, the method doesn't add the WordFragment. It is possible to use the forceAppend option to force the addition of the new content, even if the reference you indicate doesn't exist.
- If you need to set the last occurrence of an element you can use the value -1. The first element is always 1.