Cookbook
Add contents to different document types
The flexibility of javadocx allows to work very easily with any type of DOCX. That includes adding contents to documents created from scratch or to existing documents as well as modifying the contents of documents and templates.
From scratch
javadocx incorporates a base template to create new documents from scratch, that is, without any preexisting content.
With the different methods available in the javadocx API it is possible to add contents. To do this it is necessary to instantiate the CreateDocx class.
For example, to generate a new DOCX with a text paragraph, type the following code:
The base template is a DOCX file that javadocx reads to create the documents. Premium licenses let you delete the accesses to disk through the upload of this base template stored in memory.
From templates
To replace contents in an existing DOCX you have to instantiate the class CreateDocxFromTemplate, indicating the document that would serve as a template.
For example, to open the file template.docx and substitute the placeholder VAR for the text JAVADOCX, use the following code:
The templates are DOCX that exist in the file system that javadocx reads to generate documents. Premium licenses let you delete the accesses to disk through the upload of this base template stored in memory.
From scratch using base templates
Another available method that allows to work with the documents is to choose a preexisting DOCX as a base template. You can add to that template contents after its original elements.
The code that follows defines the document my_document.docx as a base template. We will add a new text after the original content:
The constant PHPDOCX_BASE_TEMPLATE is required in order to manage the default styles of javadocx.
You can either add or replace the contents in a DOCX document by instantiating the class CreateDocxFromTemplate.