Class DocxStamper
java.lang.Object
pro.verron.officestamper.core.DocxStamper
- All Implemented Interfaces:
OfficeStamper<org.docx4j.openpackaging.packages.WordprocessingMLPackage>
public class DocxStamper
extends Object
implements OfficeStamper<org.docx4j.openpackaging.packages.WordprocessingMLPackage>
The
DocxStamper class is an implementation of the OfficeStamper interface used to stamp DOCX templates with a
context object and write the result to an output stream.- Since:
- 1.0.0
- Version:
- ${version}
- Author:
- Tom Hombergs, Joseph Verron
-
Constructor Summary
ConstructorsConstructorDescriptionDocxStamper(OfficeStamperConfiguration configuration) Creates newDocxStamperwith the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionorg.docx4j.openpackaging.packages.WordprocessingMLPackageReads in a .docx template and "stamps" it, using the specified context object to fill out any expressions it finds.
-
Constructor Details
-
DocxStamper
Creates newDocxStamperwith the given configuration.- Parameters:
configuration- the configuration to use for thisDocxStamper.
-
-
Method Details
-
stamp
public org.docx4j.openpackaging.packages.WordprocessingMLPackage stamp(org.docx4j.openpackaging.packages.WordprocessingMLPackage document, Object contextRoot) Reads in a .docx template and "stamps" it, 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 notdisplayTableRowIf(boolean)to conditionally display table rows or notdisplayTableIf(boolean)to conditionally display whole tables or notrepeatTableRow(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
CommentProcessorand register it viaOfficeStamperConfiguration.addCommentProcessor(Class, CommentProcessorFactory).- Specified by:
stampin interfaceOfficeStamper<org.docx4j.openpackaging.packages.WordprocessingMLPackage>- Parameters:
document- the .docx template to stampcontextRoot- the context object to use for stamping- Returns:
- the stamped document
- Use expressions like
-