API quick guide
API quick guide
Each new version of javadocx adds features and methods to the library. After years of continuous development, the whole of the public API methods has reached such a huge number that it demands a certain knowledge to get through it.
This section summarizes the methods according to its practical utility. The complete list of methods can be found on the API documentation page.
The group of elements meant to insert new contents is one of the most extensives. Thanks to these methods it is possible to add:
- bookmarks (addBookmark)
- line and page breaks (addBreak)
- charts (addChart)
- comments (addComment)
- cross-references (addCrossReference)
- dates (addDateAndHour)
- endnotes (addEndnote)
- external files (addExternalFile)
- footnotes (addFootnote)
- form elements (addFormElement)
- headings (addHeading)
- images (addImage)
- links (addLink)
- lists (addList)
- merge fields (addMergeField)
- OLE files (addOLE)
- page numbers (addPageNumber)
- simple fields (addSimpleField)
- shapes (addShape)
- structure document tags (addStructureDocumentTag)
- tables (addTable)
- table of contents (addTableContents)
- paragraphs (addText)
- text boxes (addTextBox)
The combined use of these methods, along with the WordFragment flexibility, is the base to add contents in new documents and templates. Each method includes styling options and lots of customizing features.
The available methods aimed to work with templates allow to replace placeholders, strings and values in lists and tables, as well as more complex operations like replacing contents as a whole or inserting new elements in any place of the main document whatsoever.
These are the methods for replacing placeholders:
- replaceVariableByText, for text strings
- replacePlaceholderImage, for images
- replaceListVariable, for lists
- replaceTableVariable, for tables
- replaceVariableByWordFragment, to replace placeholders by WordFragments
DOCXPath comes with two replacing and inserting methods that offer even more flexibility: replaceWordContent, that replaces a content by a WordFragment, and insertWordFragment, to insert a WordFragment before or after an existing content.
Other methods to modify values in documents are:
- parseCheckboxes and tickCheckBoxes, ticks or unticks checkboxes
- modifyInputFields, modifies input fields values
- modifyMergeFields, modifies merge fields values
- replaceVariableByExternalFile, replaces a placeholder by a external document (only compatible with MS Word 2007 or newer)
- setTemplateSymbol, sets a new template symbol to be used for the templates
Besides the styling options found on the content methods, it is also possible to create custom styles with the following ones:
- createCharacterStyle, character styles
- createListStyle, list styles
- createParagraphStyle, paragraph styles
- createTableStyle, table styles
- setDefaultFont, to choose the default font
- setDocumentDefaultStyles to set the global default styles of the document
- embedFont, embeds a font
In case of needing to import custom styles from existing DOCX instead of creating them dinamically, the next method can do the task: importStyles.
Lastly, the customizeWordContent method can change existing styles of a document on the fly.
The group of methods that define and customize the layout of a document is divided between the ones that assign global and particular properties (addProperties, docxSettings), add and modify line numbering (setLineNumbering), insert colors and borders (addPageBorders and setBackgroundColor), establish paper type, size and margins (modifyPageLayout) and apply rtl languages (setRTL).
Images can be added to a document with addImage or as a background with the help of addBackgroundImage and watermarkDocx.
On the other hand, shapes are added by calling the addShape method.
The addChart method allows to place graphs by setting multiple parameters to define their properties, styles, generate combo charts, trend lines and many more options.
MathML is a standard format to work with maths equations. MS Word uses OMML as its equations language.
javadocx includes addMathEquation to convert MathML equations to OMML, and transformOMMLToMathML for the opposite operation.
It is possible to obtain information of a document and its content thanks to several methods:
- indexer, extracts contents
- getTemplateVariables, gets the template placeholders
- getTemplateVariablesType, gets the template placeholders and their types
- getWordContents, returns text contents of a DOCXPath query
- getDocxPathQueryInfo, information from a DOCXPath query
Blocks allow to demarcate content groups that can be deleted with deleteTemplateBlock or cloned with cloneBlock. To clean blocks placeholders, use clearBlocks.
The way to transform HTML and CSS to Word is by using the method embedHtml. For templates, the required method is replaceVariableByHTML.
With the help of the available methods it is possible to convert from DOCX to PDF through transformDocument.
Optimum performance when generating a DOCX can be achieved with the stream mode, which doesn't generate documents in the file system but in memory, which returns them directly as a stream.
The available methods to work with sections allow to create new ones (addSection) and delete them (removeSection).
It is also feasible to merge several documents while retaining its contents with mergeDocx.
The addHeader and addFooter methods add headers and footers respectively.
It is viable either to merge PDF documents with mergePDF, to divide them by pages with splitPDF or to remove pages using removePages.
transformDocument, a method included with the conversion plugin, transforms document formats as DOCX, ODT, RTF and others to PDF while keeping styles and contents, as long as the format grants it.
encryptPDF method encrypt PDF documents.
Finally, watermarkPdf adds texts and images as document watermarks.
To move or clone contents in templates or documents created from scratch, DOCXPath provides two powerful methods: cloneWordContent, which clone contents, and moveWordContent, which move them.
If it is necessary to delete contents, the following methods are ready for use:
- removeWordContent, deletes content with a DOCXPath query
- removeFooters, deletes footers
- removeHeaders, deletes headers
- removeTemplateVariable, deletes placeholders along with the template symbols
- removeChapter, deletes a whole chapter
- watermarkRemove, erases a watermark
In case of need of inserting some OOXML directly on a document, it is possible to use addWordML or replace a placeholder with replaceVariableByWordML.