antonmihaylov / OpenXmlTemplates

Word .docx templating system that is designer (no scripting tags) and server-friendly (no word installation required)
GNU Lesser General Public License v3.0
92 stars 25 forks source link
docx docx-generator docx-template docxtemplater microsoft-word microsoft-word-automation word

GitHub contributors GitHub issues License: LGPL v3


Open XML Templates

A .NET Standard Word documents templating system that doesn't need Word installed and is both designer and developer friendly

Report Bug · Request Feature

Table of Contents

About The Project

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.

Built With

Getting Started

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

Usage

To create a template:

  1. Open your document in Word
  2. Open the Developer tab in the ribbon (if you don't have it - open File tab, go to Options > Customize Ribbon. Under Customize the Ribbon and under Main Tabs, select the Developer check box.)
  3. Under the Controls tab - add a new Content Control of your liking (Plain text is the simplest one - just text with formatting)
  4. Select the newly added Content control and click Properties in the Developer ribbon
  5. Change the Tag in the popup window to match one of the supported tags (the tag name is case-insensitive - variable is the same as VARIABLE)

To create a document from a template, using the default content control replacers:

  1. Create a new TemplateDocument. This represents your document and it neatly handles all content controls in it, as well as the open/save/close file logic. Don't forget to call Dispose() on it after you're done, or just use an "using" statement:
         using var doc = new TemplateDocument("path/to/my/document.docx");
  2. Create a new VariableSource (currently available sources are a json string and a dictionary. You can also create your own class that implements IVariableSource). The variable source handles your data and extracts it in a way that the template engine can read it.
        var src = new VariableSource(jsonString); 
  3. Create an OpenXmlTemplateEngine. A default one is provided (DefaultOpenXmlTemplateEngine). The default one contains all control replacers listed in the readme. You can disable/enable a control replacer by modifying the IsEnabled variable in it. You can also register your own replacer by calling RegisterReplacer on the engine.
        var engine = new DefaultOpenXmlTemplateEngine();
  4. Call the ReplaceAll method on the engine using the document and the variable source
        engine.ReplaceAll(doc, src);
  5. 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:

    1. Use the RemoveControlsAndKeepContent method on the TemplateDocument object or
    2. Set the KeepContentControlAfterReplacement boolean of the OpenXmlTemplateEngine

Supported Tags

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

Variable

Conditional remove

Singular dropdown

Conditional dropdown

Images

Any standard .net numeric formatter may be used. Numeric Formatters

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the LGPLv3 License. See LICENSE for more information.

Contact

Anton Mihaylov - antonmmihaylov@gmail.com

Project Link: https://github.com/antonmihaylov/OpenXmlTemplates