Sample Document Template generation using Docx4j
Docx4j is an open source (ASLv2) Java library for creating and manipulating Microsoft Open XML (Word docx, Powerpoint pptx, and Excel xlsx) files.Download Docx4j and dependices here Jar's and add them in Libraries and Classpath
Download OpenDoPE Microsoft word extension and run extension.You should OpenDoPE extension on Microsoft as below
Save the Template you want to create first . Click on 'Bind this text' for the text you wanted to generate dynamically.
Click Bind this.. on the extension panel
Then it should open a XML text editor, paste the XML .which we will be generate using JAXBMarshalling
Select the XML element to Bind with , enter a unique XPath ID and Click Use this XPath
Repeat same for other elements also and template should look like below
Drag and drop EmployeeVO as a form , create a button , Action Listener for that button and change text to 'Create Template' .
Below is code snippet for ActionListener , which gets current row from EmployeesVO and set's Person Object with First Name, Last Name, Email and Phone. Using JAXBMarshalling current row is converted into XML String .
Below snippet code is for binding template with XML String
Finally,After generating Word document will replace the text with dynamic data.
Happy Learning!!!
No comments:
Post a Comment