A .NET Standard Word documents templating system that doesn't need Word installed and is both designer and developer friendly
Report Bug
·
Request Feature
With the library you can easily:
It is server-friendly, because it doesn't require Word installed. Only the Open XML Sdk is used for manipulating the document.
It is friendly to the designer of the document templates, because they don't need to have any coding skills and they won't have to write any script-like snippets in the word document. Everything is instead managed by native Word content controls.
To get a local copy up and running use one of the following methods:
Install via nuget:
nuget install OpenXMLTemplates
or clone the repo and reference OpenXMLTemplates.csproj in your project
git clone https://github.com/antonmihaylov/OpenXmlTemplates.git
using var doc = new TemplateDocument("path/to/my/document.docx");
var src = new VariableSource(jsonString);
var engine = new DefaultOpenXmlTemplateEngine();
engine.ReplaceAll(doc, src);
Save the edited document
doc.SaveAs("result.docx");
If you want to remove the content controls from the final document, but keep the content you have two options:
Note that if your variable names contain an underscore results may be unpredictable! Note: to insert a new line, add a new line character (\r\n, \n\r, \n) in the data you provide, it will be parsed as a line break
Tag name: "variable_\<NAME OF YOUR VARIABLE>" (the variable keyword is case-insensitive)
Replaces the text inside the control with the value of the variable with the provided name
Supports nested variable names (e.g. address.street)
Supports array access (e.g. names.[0])
Supports nested variables using rich text content controls. For example: a rich text content control with tag name address, followed by an inner content control with tag name variable_street is the same as variable.street
Note that if you reference a variable from a nested control, that is available in the outer scope, but not in the inner scope - the outer scope variable will be used.
Supports variables inside repeating items, the variable name is relative to the repeated item.
Example:
Tag name: "repeating_\<NAME OF YOUR VARIABLE>" (the repeating keyword is case-insensitive)
Repeats the content control as many times as there are items in the variable identified by the provided variable name.
Complex fields with inner content controls are supported. Use the inner controls as you would normally, except that the variable names will be relative to the list item. All default content controls can be nested.
Note that if you reference a variable from a nested control, that is available in the outer scope, but not in the inner scope (the list item) - the outer scope variable will be used. That is useful if you want to include something in your list item's text output that is available in the global scope only.
Add an inner content control with tag variable_index to insert the index of the current item (1-based)
You can add extra arguments to the tag name (e.g. "repeating_\<VARIABLE NAME>_extraparam1_extraparam2..."):
Example:
You can also chain multiple arguments, e.g. "conditionalRemove_\<VARIABLE NAME 1>_not_or__\<VARIABLE NAME 2>and\<VARIABLE NAME 3>". Note that the expression is evaluated from left to right, with no recognition for the order of operations.
Example:
If the list variable has a length of 1 (or 0) the first value from the dropdown is used. If it's more than one - the second value from the dropdown is used.
Example:
You can use the same extra arguments as in the Conditional remove replacer
Put an image path as a variable in your data, the replacer will get the image and will embed it in the Image content control.
Variables that are numeric can be formatted. Follow the variable with () and include the formmater inside the parenthesis. Example: variable_thing1(N2)
Any standard .net numeric formatter may be used. Numeric Formatters
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Distributed under the LGPLv3 License. See LICENSE
for more information.
Anton Mihaylov - antonmmihaylov@gmail.com
Project Link: https://github.com/antonmihaylov/OpenXmlTemplates