API quick guide
API quick guide
Each new version of phpdocx 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)
- 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:
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
- replaceVariableByExternalFile, replaces a placeholder by a external document (only compatible with MS Word 2007 or newer)
- searchAndHighlight, searches and highlights strings
- searchAndReplace, searches and replaces strings
- 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
- getWordStyles, returns the styles used by contents
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 and modifyDocxSettings), add and modify line numbering (addLineNumbering and setLineNumbering), insert colors and borders (addPageBorders and setBackgroundColor), establish paper type, size and margins (modifyPageLayout) and apply rtl languages (setRTL).
MathML is a standard format to work with maths equations. MS Word uses OMML as its equations language.
phpdocx includes addMathEquation to convert MathML equations to OMML, and transformOMMLToMathML for the opposite operation. The latter method is the one transformDocAdvHTML calls to transform the contents of a DOCX to HTML.
It is possible to obtain information of a document and its content thanks to several methods:
- indexer, extracts contents, styles, images, people...
- getStatistics, document statistics: pages, words, images...
- getTemplateVariables, gets the template placeholders
- getWordContents, returns text contents of a DOCXPath query
- getDocxPathQueryInfo, information from a DOCXPath query
- getWordFiles, returns file contents from the document
- getWordStyles, returns styles used by contents of a DOCXPath query
The way to transform HTML and CSS to Word is by using the method embedHtml. For templates, the required method is replaceVariableByHTML. Premium licenses include HTML Extended, to invoke phpdocx methods with custom HTML tags.
The opposite conversion (DOCX to HTML/CSS) is performed with transformDocAdvHTML.
With the help of the available methods it is possible to convert from one document format to another, from the easier tasks, TXT <=> DOCX with DOCX2TXT and TXT2DOCX to more complex transformations from DOCX to PDF, DOC <=> DOCX and others like RTF and ODT through transformDocument, and DOCX <=> HTML with embedHtml and transformDocAdvHTML.
In order to obtain maximum performance when working with templates, the optimizeTemplate method repairs all placeholders, which skips the whole parsing of XML of contents for each placeholder, spedding up the use of the template methods.
The parseDocx method generates and serializes in-memory DOCX in order to save and reuse them later.
Optimum performance when generating a DOCX can be achieved with zipStream, which doesn't generate documents in the file system but in memory, which returns them directly as a stream.
Existing DOCX documents can be optimized using optimizeDocx.
In case of need of protection just for specific parts of a document, the method addPermProtection allows to do it.
It is also feasible to merge several documents while retaining its contents with mergeDocx.
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.
Finally, watermarkPdf adds texts and images as document watermarks.
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
- searchAndRemove, searches and deletes text strings
- removeTemplateVariable, deletes placeholders along with the template symbols
- removeChapter, deletes a whole chapter
- watermarkRemove, erases a watermark
Some specific methods are available to track added, replaced or modified contents in a document, even accepting or rejecting changes.
The enableTracking method activates change tracking. After activation, the compatible methods include the corresponding tracking tags. E.g., when inserting paragraphs or deleting contents. The disableTracking method deactivates tracking.
The addPerson method includes the people who participate in the document.
Besides all the methods above mentioned, phpdocx includes many others like: rawSearchAndReplace, that replaces strings in any XML present in the DOCX, setEncodeUTF8, that turns text encoding to UTF8, setLanguage, that changes default language or addMacroFromDoc to add an existing macro to a DOCX.
Document generation is done with createDocx. This method creates the document in the file system. To create a document and download it afterwards, run createDocxAndDownload. Generation as a stream is available with zipStream.
The API page of each method details for which license(s) is available.