Skip to content

Latest commit

History

89 Commits

Folders and files

NameName
Last commit message
Last commit date

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

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newFieldContent("Report date",DateTime.Now.ToString()));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling tables

filling table

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newTableContent("Team Members Table").AddRow(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager")).AddRow(newFieldContent("Name","Bob"),newFieldContent("Role","Developer")),newFieldContent("Count","2"));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling lists

filling list

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newListContent("Team Members List").AddItem(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager")).AddItem(newFieldContent("Name","Bob"),newFieldContent("Role","Developer")));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling nested lists

filling nested list

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newListContent("Team Members Nested List").AddItem(newListItemContent("Role","Program Manager").AddNestedItem(newFieldContent("Name","Eric")).AddNestedItem(newFieldContent("Name","Ann"))).AddItem(newListItemContent("Role","Developer").AddNestedItem(newFieldContent("Name","Bob")).AddNestedItem(newFieldContent("Name","Richard"))));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling list inside table

filling list inside table

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newTableContent("Projects Table").AddRow(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager"),newListContent("Projects").AddItem(newFieldContent("Project","Project one")).AddItem(newFieldContent("Project","Project two"))).AddRow(newFieldContent("Name","Bob"),newFieldContent("Role","Developer"),newListContent("Projects").AddItem(newFieldContent("Project","Project one")).AddItem(newFieldContent("Project","Project three"))));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling table inside list

filling table inside list

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newListContent("Projects List").AddItem(newListItemContent("Project","Project one").AddTable(TableContent.Create("Team members").AddRow(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager")).AddRow(newFieldContent("Name","Bob"),newFieldContent("Role","Developer")))).AddItem(newListItemContent("Project","Project two").AddTable(TableContent.Create("Team members").AddRow(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager")))).AddItem(newListItemContent("Project","Project three").AddTable(TableContent.Create("Team members").AddRow(newFieldContent("Name","Bob"),newFieldContent("Role","Developer")))));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling table with several blocks

filling table with several blocks

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newTableContent("Team Members Statistics").AddRow(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager")).AddRow(newFieldContent("Name","Richard"),newFieldContent("Role","Program Manager")).AddRow(newFieldContent("Name","Bob"),newFieldContent("Role","Developer")),newTableContent("Team Members Statistics").AddRow(newFieldContent("Statistics Role","Program Manager"),newFieldContent("Statistics Role Count","2")).AddRow(newFieldContent("Statistics Role","Developer"),newFieldContent("Statistics Role Count","1")));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling table with merged rows

filling table with merged rows

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newTableContent("Team members info").AddRow(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager"),newFieldContent("Age","37"),newFieldContent("Gender","Male")).AddRow(newFieldContent("Name","Bob"),newFieldContent("Role","Developer"),newFieldContent("Age","33"),newFieldContent("Gender","Male")).AddRow(newFieldContent("Name","Ann"),newFieldContent("Role","Developer"),newFieldContent("Age","34"),newFieldContent("Gender","Female")));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling table with merged columns

table with merged columns

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newTableContent("Team members projects").AddRow(newFieldContent("Name","Eric"),newFieldContent("Role","Program Manager"),newFieldContent("Age","37"),newFieldContent("Projects","Project one, Project two")).AddRow(newFieldContent("Name","Bob"),newFieldContent("Role","Developer"),newFieldContent("Age","33"),newFieldContent("Projects","Project one")).AddRow(newFieldContent("Name","Ann"),newFieldContent("Role","Developer"),newFieldContent("Age","34"),newFieldContent("Projects","Project two")));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling images

filling images

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newImageContent("photo",File.ReadAllBytes("Tesla.jpg")));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling images inside a table

filling images inside a table

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newTableContent("Scientists Table").AddRow(newFieldContent("Name","Nicola Tesla"),newFieldContent("Born",newDateTime(1856,7,10).ToShortDateString()),newImageContent("Photo",File.ReadAllBytes("Tesla.jpg")),newFieldContent("Info","Serbian American inventor, electrical engineer, mechanical engineer, physicist, and futurist best known for his contributions to the design of the modern alternating current (AC) electricity supply system")).AddRow(newFieldContent("Name","Thomas Edison"),newFieldContent("Born",newDateTime(1847,2,11).ToShortDateString()),newImageContent("Photo",File.ReadAllBytes("Edison.jpg")),newFieldContent("Info","American inventor and businessman. He developed many devices that greatly influenced life around the world, including the phonograph, the motion picture camera, and the long-lasting, practical electric light bulb.")).AddRow(newFieldContent("Name","Albert Einstein"),newFieldContent("Born",newDateTime(1879,3,14).ToShortDateString()),newImageContent("Photo",File.ReadAllBytes("Einstein.jpg")),newFieldContent("Info","German-born theoretical physicist. He developed the general theory of relativity, one of the two pillars of modern physics (alongside quantum mechanics). Einstein's work is also known for its influence on the philosophy of science. Einstein is best known in popular culture for his mass–energy equivalence formula E = mc2 (which has been dubbed 'the world's most famous equation').")));using(varoutputDocument=newTemplateProcessor("OutputDocument.docx").SetRemoveContentControls(true)){outputDocument.FillContent(valuesToFill);outputDocument.SaveChanges();}}}}

###Filling images inside a list

filling images inside a list

usingSystem;usingSystem.IO;namespaceTemplateEngine.Docx.Example{classProgram{staticvoidMain(string[]args){File.Delete("OutputDocument.docx");File.Copy("InputTemplate.docx","OutputDocument.docx");varvaluesToFill=newContent(newListContent("Scientists List").AddItem(newFieldContent("Name","Nicola Tesla"),newImageContent("Photo",File.ReadAllBytes("Tesla.jpg")),newFieldContent("Dates of life",string.Format("{0}-{1}",1856,1943)),newFieldContent("Info","Serbian American inventor, electrical engineer, mechanical engineer, physicist, and futurist best known for his contributions to the design of the modern alternating current (AC) electricity supply system")).AddItem(newFieldContent("Name","Thomas Edison"),newImageContent("Photo",File.ReadAllBytes("Edison.jpg")),newFieldContent("Dates of life",string.Format("{0}-{1}",1847,1931)),newFieldContent("Info","American inventor and businessman. He developed many devices that greatly influenced life around the world, including the phonograph, the motion picture camera, and the long-lasting, practical electric light bulb.")).AddItem(newFieldContent("Name","Albert Einstein"),newImageContent("Photo",File.ReadAllBytes("Einstein.jpg")),newFieldContent("Dates of life",string.Format("{0}-{1}",1879,1955)),newFieldContent("Info","German-born theoretical physicist. He developed the general theory of relativity, one of the two pillars of modern physics (alongside quantum mechanics). Einstein's work is also known for its influence on the philosophy of science. Einstein is best known in popular culture for his mass–energy equivalence formula E = mc2 (which has been dubbed 'the world's most famous equation').")));using(varoutputDocument=newTemplateProcessor("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

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages