OpenXmlPowerTools provides guidance and example code for programming with Open XML Documents (DOCX, XLSX, and PPTX). It is based on, and extends the functionality of the Open XML SDK.
The DocumentAssembler module is a powerful tool for generating documents from templates and data. It allows you to create .docx files with dynamic content, such as tables, conditional sections, and repeating elements, based on data from an XML file.
- Template-Based Document Generation: Create documents from Word templates (
.docx) and populate them with data from XML files. - Content Replacement: Use simple placeholders in your template to insert data from your XML file.
- Dynamic Tables: Automatically generate tables in your document based on data from your XML file.
- Conditional Content: Include or exclude parts of your document based on conditions in your data.
- Repeating Content: Repeat sections of your document for each item in a collection in your data.
- Error Handling: The
DocumentAssemblerwill report errors in the generated document if it encounters any issues with your template or data.
The DocumentAssembler works by processing a Word document that contains special markup in content controls or in paragraphs. This markup defines how the document should be assembled based on the provided XML data.
The process is as follows:
- Create a Template: Start with a regular Word document (
.docx). - Add Placeholders: Use content controls or special syntax in paragraphs to define placeholders for your data.
- Provide Data: Create an XML file that contains the data you want to insert into the document.
- Assemble the Document: Use the
DocumentAssembler.AssembleDocumentmethod to merge the template and data, producing a new Word document.
The template syntax uses XML elements within content controls or as text in the format <#ElementName ... #>.
To replace a placeholder with a value from your XML data, you can use the Content element. The Select attribute contains an XPath expression to select the data from the XML file.
Example:
<#Content Select="Customer/Name" #>
To generate a table, you use the Table element. The Select attribute specifies the collection of data to iterate over. The table in the template must have a prototype row, which will be repeated for each item in the data.
Example:
<#Table Select="Customers/Customer" #>
You can conditionally include content using the Conditional element. The Select attribute specifies the data to test, and the Match or NotMatch attribute specifies the value to compare against.
Example:
<#Conditional Select="Customer/Country" Match="USA" #> ... content to include if the customer is from the USA ... <#EndConditional #>
To repeat a section of the document, you can use the Repeat element. The Select attribute specifies the collection of data to iterate over.
Example:
<#Repeat Select="Customers/Customer" #> ... content to repeat for each customer ... <#EndRepeat #>
To use the DocumentAssembler, you will need to:
- Add a reference to the
OpenXmlPowerToolslibrary in your project. - Create a Word template with the appropriate placeholders.
- Create an XML data file.
- Call the
DocumentAssembler.AssembleDocumentmethod to generate your document.
For more detailed examples and documentation, please refer to the DocumentAssembler, DocumentAssembler01, DocumentAssembler02, and DocumentAssembler03 projects in the OpenXmlPowerToolsExamples directory.