Skip to content

BorisVaskin/TemplateEngine.Docx

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TemplateEngine.Docx

Template engine for generating Word docx documents on the server-side with a human-created Word templates, based on content controls Word feature.

Based on Eric White code sample.

Installation

You can get it with NuGet package.

nuget install command

Example of usage

Here is source code of sample usage.

How to create template

  • Create Word document with prototype of content:
    document example

  • Switch to developer ribbon tab. If you can't see it, go to settings of ribbon menu and enable developer tab:
    developer mode

  • All data binds on content control tag value:
    create content control

###Filling fields

docx template

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
	        var valuesToFill = new Content(
		        new FieldContent("Report date", DateTime.Now.ToString()));

		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling tables

filling table

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
		        new TableContent("Team Members Table")
			        .AddRow(
				        new FieldContent("Name", "Eric"),
				        new FieldContent("Role", "Program Manager"))
			        .AddRow(
				        new FieldContent("Name", "Bob"),
				        new FieldContent("Role", "Developer")),

		        new FieldContent("Count", "2"));
		
		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling lists

filling list

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
		        new ListContent("Team Members List")
					.AddItem(
						new FieldContent("Name", "Eric"), 
						new FieldContent("Role", "Program Manager"))
					.AddItem(
						new FieldContent("Name", "Bob"),
						new FieldContent("Role", "Developer")));

		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling nested lists

filling nested list

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
		       	new ListContent("Team Members Nested List")
					.AddItem(new ListItemContent("Role", "Program Manager")
						.AddNestedItem(new FieldContent("Name", "Eric"))
						.AddNestedItem(new FieldContent("Name", "Ann")))
					.AddItem(new ListItemContent("Role", "Developer")
						.AddNestedItem(new FieldContent("Name", "Bob"))
						.AddNestedItem(new FieldContent("Name", "Richard"))));


		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling list inside table

filling list inside table

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
		       new TableContent("Projects Table")
					.AddRow(
						new FieldContent("Name", "Eric"), 
						new FieldContent("Role", "Program Manager"), 
						new ListContent("Projects")
							.AddItem(new FieldContent("Project", "Project one"))
							.AddItem(new FieldContent("Project", "Project two")))
					.AddRow(
						new FieldContent("Name", "Bob"),
						new FieldContent("Role", "Developer"),
						new ListContent("Projects")
							.AddItem(new FieldContent("Project", "Project one"))
							.AddItem(new FieldContent("Project", "Project three"))));


		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling table inside list

filling table inside list

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
		      new ListContent("Projects List")
					.AddItem(new ListItemContent("Project", "Project one")
						.AddTable(TableContent.Create("Team members")
							.AddRow(
								new FieldContent("Name", "Eric"), 
								new FieldContent("Role", "Program Manager"))
							.AddRow(
								new FieldContent("Name", "Bob"), 
								new FieldContent("Role", "Developer"))))
					.AddItem(new ListItemContent("Project", "Project two")
						.AddTable(TableContent.Create("Team members")
							.AddRow(
								new FieldContent("Name", "Eric"),
								new FieldContent("Role", "Program Manager"))))
					.AddItem(new ListItemContent("Project", "Project three")
						.AddTable(TableContent.Create("Team members")
							.AddRow(
								new FieldContent("Name", "Bob"),
								new FieldContent("Role", "Developer")))));


		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling table with several blocks

filling table with several blocks

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
				new TableContent("Team Members Statistics")
					.AddRow(
						new FieldContent("Name", "Eric"),
						new FieldContent("Role", "Program Manager"))
					.AddRow(
					    new FieldContent("Name", "Richard"),
						new FieldContent("Role", "Program Manager"))
					.AddRow(
						new FieldContent("Name", "Bob"),
						new FieldContent("Role", "Developer")),

				new TableContent("Team Members Statistics")
					.AddRow(
						new FieldContent("Statistics Role", "Program Manager"),
						new FieldContent("Statistics Role Count", "2"))						
					.AddRow(
						new FieldContent("Statistics Role", "Developer"),
						new FieldContent("Statistics Role Count", "1")));


		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling table with merged rows

filling table with merged rows

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
				new TableContent("Team members info")
					.AddRow(
						new FieldContent("Name", "Eric"),
						new FieldContent("Role", "Program Manager"),
						new FieldContent("Age", "37"),
						new FieldContent("Gender", "Male"))
					.AddRow(
						new FieldContent("Name", "Bob"),
						new FieldContent("Role", "Developer"),
						new FieldContent("Age", "33"),
						new FieldContent("Gender", "Male"))
					.AddRow(
						new FieldContent("Name", "Ann"),
						new FieldContent("Role", "Developer"),
						new FieldContent("Age", "34"),
						new FieldContent("Gender", "Female")));


		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Filling table with merged columns

table with merged columns

using System;
using System.IO;

namespace TemplateEngine.Docx.Example
{
    class Program
    {
        static void Main(string[] args)
        {				
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");
		
			var valuesToFill = new Content(
				new TableContent("Team members projects")
					.AddRow(
						new FieldContent("Name", "Eric"),
						new FieldContent("Role", "Program Manager"),
						new FieldContent("Age", "37"),
						new FieldContent("Projects", "Project one, Project two"))
					.AddRow(
						new FieldContent("Name", "Bob"),
						new FieldContent("Role", "Developer"),
						new FieldContent("Age", "33"),
						new FieldContent("Projects", "Project one"))
					.AddRow(
						new FieldContent("Name", "Ann"),
						new FieldContent("Role", "Developer"),
						new FieldContent("Age", "34"),
						new FieldContent("Projects", "Project two")));


		    using(var outputDocument = new TemplateProcessor("OutputDocument.docx")
				.SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            } 
		}
	}
}			

###Content structure content structure

Have fun!

yandex counter

About

Smart docx template engine for .NET

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%