ThinkSharp.WordTemplating is a lightweight templating engine for OpenXml (docx) documents with simple replacement capabilities based on DocumentFormat.OpenXml.
Usage:
The library is basically, one class that can be copied and modified if needed. However, there is also a nuget version: Nuget
Install-Package ThinkSharp.WordTemplating
var replacements = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
replacements.Add("Replacement1", "Text For Replacement1");
replacements.Add("Replacement2", "Text For Replacement2");
replacements.Add("MyInt", 5);
replacements.Add("MyDate", DateTime.Now);
replacements.Add("MyDouble", Math.PI);
replacements.Add("MyBoolean", true);
replacements.Add("MyDateTimeOffset", DateTimeOffset.Now);
replacements.Add("CustomType", new CustomType { FirstName = "Hugo", LastName = "Boss" } );
replacements.Add("CustomTypeNoConverter", new CustomTypeNoConverter { FirstName = "Hugo", LastName = "Boss" });
// add custom converters (overwrites default converts)
engine.AddReplacementConverter<bool>((val, format) => format == "YesNo" ? (val ? "Yes" : "No") : val.ToString());
engine.AddReplacementConverter<CustomType>((val, format) => format == "LastFirst" ? $"{val.LastName}, {val.FirstName}" : val.ToString());
var engine = new TemplateEngine();
// optional: change culture for value conversation. Default is "en-EN"
// engine.Culture = new CultureInfo("de-DE")
// optional: change the value for missing values in replacement dicationary. Default ist: NoReplacementValueAvailable
// engone.NoReplacementAvailableValue = ""
engine.ReplaceTemplate(@"Data\Test.docx", replacements);
public class CustomType
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString() => $"{FirstName} {LastName}";
}
public class CustomTypeNoConverter
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString() => $"{FirstName} {LastName}";
}
Placeholders in Word have the pattern "<name:format>". For common types (DateTime, int, double, ...) .Net conventions for the format will be used. However, it is possible to register custom converters that evaluate custom format strings.
The code converting the following docx template
to the that one:
ThinkSharp.WordTemplating is released under The MIT license (MIT)
We use SemVer for versioning. For the versions available, see the tags on this repository.
If you like ThinkSharp.WordTemplating and use it in your project(s), feel free to give me a cup of coffee :)