Package org.wickedsource.docxstamper
Class DocxStamper<T>
java.lang.Object
org.wickedsource.docxstamper.DocxStamper<T>
- Type Parameters:
T- the class of the context object used to resolve expressions against.
Main class of the docx-stamper library. This class can be used to "stamp" .docx templates to create a .docx document filled with custom data at runtime.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidstamp(InputStream template, T contextRoot, OutputStream out) Reads in a .docx template and "stamps" it into the given OutputStream, using the specified context object to fill out any expressions it finds.voidstamp(org.docx4j.openpackaging.packages.WordprocessingMLPackage document, T contextRoot, OutputStream out) Same as stamp(InputStream, T, OutputStream) except that you may pass in a DOCX4J document as a template instead of an InputStream.
-
Constructor Details
-
DocxStamper
public DocxStamper() -
DocxStamper
-
-
Method Details
-
stamp
public void stamp(InputStream template, T contextRoot, OutputStream out) throws DocxStamperException Reads in a .docx template and "stamps" it into the given OutputStream, using the specified context object to fill out any expressions it finds.
In the .docx template you have the following options to influence the "stamping" process:
- Use expressions like ${name} or ${person.isOlderThan(18)} in the template's text. These expressions are resolved against the contextRoot object you pass into this method and are replaced by the results.
- Use comments within the .docx template to mark certain paragraphs to be manipulated.
Within comments, you can put expressions in which you can use the following methods by default:
- displayParagraphIf(boolean) to conditionally display paragraphs or not
- displayTableRowIf(boolean) to conditionally display table rows or not
- displayTableIf(boolean) to conditionally display whole tables or not
- repeatTableRow(List<Object>) to create a new table row for each object in the list and resolve expressions within the table cells against one of the objects within the list.
If you need a wider vocabulary of methods available in the comments, you can create your own ICommentProcessor and register it via getCommentProcessorRegistry().addCommentProcessor().
- Parameters:
template- the .docx template.contextRoot- the context root object against which all expressions found in the template are evaluated.out- the output stream in which to write the resulting .docx document.- Throws:
DocxStamperException- in case of an error.
-
stamp
public void stamp(org.docx4j.openpackaging.packages.WordprocessingMLPackage document, T contextRoot, OutputStream out) throws DocxStamperException Same as stamp(InputStream, T, OutputStream) except that you may pass in a DOCX4J document as a template instead of an InputStream.- Parameters:
document- the .docx template.contextRoot- the context root object against which all expressions found in the template are evaluated.out- the output stream in which to write the resulting .docx document.- Throws:
DocxStamperException- in case of an error.
-