Git Product home page Git Product logo

thinksharp.wordtemplating's Introduction

ThinkSharp.WordTemplating

NuGet License Donate

Introduction

ThinkSharp.WordTemplating is a lightweight templating engine for OpenXml (docx) documents with simple replacement capabilities based on DocumentFormat.OpenXml.

Usage:

Installation

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

Usage

    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

grafik

to the that one:

grafik

License

ThinkSharp.WordTemplating is released under The MIT license (MIT)

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Donation

If you like ThinkSharp.WordTemplating and use it in your project(s), feel free to give me a cup of coffee :)

paypal

thinksharp.wordtemplating's People

Contributors

jandotnet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.