//! Constructor of WordGenerator. <param name="path">Elérési út</param> /// <summary> /// Constuctor of WorldGenerator /// </summary> /// <example> This sample shows how to call the WordGenerator method. /// <code> /// class MyClass /// { /// public static int Main() /// { /// DocumentGenerator g = new WorldGenerator("C:\\", true) /// } /// } /// </code> /// </example> /// <param name="path"><summary>The path of the document</summary><remarks>There would be generated the new document.</remarks></param> /// <param name="showWord">Should the Word be visible?</param> /// <param name="template">The name of the template, what the program uses</param> public WordGenerator(string path, bool showWord = true, WordTemplate template = WordTemplate.Default) { currentTemplate = template; this.path = path; this.ShowWord = showWord; this.BeginDocument(); }
public ITemplate CreateTemplate(byte[] templateBytes) { var template = new WordTemplate(templateBytes); template.LoadSections(); return(template); }
protected void Page_Load(object sender, EventArgs e) { try { //load the web object for the site that the page is now in context of using (SPWeb web = SPControl.GetContextWeb(Context)) { //load the list that was passed in the 'list' querystring parameter to the page SPList list = web.Lists[new Guid(Page.Request.QueryString["list"])]; SPFile template = Web.GetFile(Page.Request.QueryString["TemplateLocation"]); SPListItem item = list.GetItemById(int.Parse(Page.Request.QueryString["Item"])); Dictionary<string, object> dat = getData(item); WordTemplate wt = new WordTemplate(); wt.Open(template.OpenBinaryStream()); wt.SetDataSource(dat.Values.ToArray(), dat.Keys.ToArray(), "data"); wt.Process(); wt.Save(Page.Response, template.Name, false); } } catch (Exception ex) { ErrorText.Text = ex.Message; } }
public void ReplaceTemplate() { WordTemplate template = new WordTemplate(); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("a0101", "123"); dic.Add("b0110", "信息系统不"); template.ReplaceTemplate("E:\\离职交接表.docx", "E:\\离职交接表1.docx", dic); Assert.True(true); }
string sourceColPattern = @"^[a-zA-Z][\s\w]*[a-zA-Z0-9]\.[a-zA-Z][\s\w]*[a-zA-Z0-9]$"; //«DataSource.ColumnName» or «[DataSource].[ColumnName]» or «[Data Source].[Column Name]» /// <summary> /// GenerateBind() /// This method is the only method that needs to be called. It calls all the other methods necessary for generating data. /// </summary> /// <param name="wt">Template object with an open excel file</param> public void GenerateBind(WordTemplate wt) { // Stores the template for use in other methods this.wt = wt; // Gets the the merge fields and bookmarks allMergefields = wt.FieldMarkers; allBookmarks = wt.Bookmarks; // Parse Fields parseFieldsDocLevel(); parseFieldsInBookmarks(); }
public static void Main(String[] args) { // Create an instance of WordTemplate and open the template file WordTemplate wt = new WordTemplate(); string file = "Bookmark.docx"; wt.Open(@"..\..\TestFiles\" + file); // Crate an instance of the DataGenerator WordTemplateDataGenerator dg = new WordTemplateDataGenerator(); dg.GenerateBind(wt); wt.Process(); wt.Save(@"../../Output/Output_" + file); }
static void Main(string[] args) { var data = new { teacher = "Ben", author = "John Doe", students = new[] { new { name = "Foo", age = 15 }, new { name = "Bar", age = 16 }, } }; var images = new Dictionary <string, byte[]> { ["signature_pic"] = File.ReadAllBytes("signature.png") }; var reporter = WordTemplate.FromFile("Template.docx"); var mem = new MemoryStream(); reporter.Render(mem, data, images); File.WriteAllBytes("Output.docx", mem.ToArray()); }
public void PrintWordTemplate(string templateFile, string outputFile, IDictionary <string, string> keyValues) { WordTemplate wordTemplate = new WordTemplate(); wordTemplate.ReplaceTemplate(templateFile, outputFile, keyValues); }
static void Main(string[] args) { var data = new WordTemplateData { new FieldData { TagName = "World", Value = "people" }, new CollectionData { TagName = "Chapter", ElementData = new List <WordTemplateData> { new WordTemplateData { new FieldData { TagName = "Date", Value = DateTime.Now.ToShortDateString() }, new FieldData { TagName = "Name", Value = "Name 1" }, new CollectionData { TagName = "SubChapter", ElementData = new List <WordTemplateData> { new WordTemplateData { new FieldData { TagName = "Change", Value = Guid.NewGuid().ToString() } }, new WordTemplateData { new FieldData { TagName = "Change", Value = Guid.NewGuid().ToString() } } } } }, new WordTemplateData { new FieldData { TagName = "Date", Value = DateTime.Now.ToShortDateString() }, new FieldData { TagName = "Name", Value = "Name 222" }, new CollectionData { TagName = "SubChapter", ElementData = new List <WordTemplateData> { new WordTemplateData { new FieldData { TagName = "Change", Value = Guid.NewGuid().ToString() } } } } }, new WordTemplateData { new FieldData { TagName = "Date", Value = DateTime.Now.AddDays(30).ToShortDateString() }, new FieldData { TagName = "Name", Value = "Name 3" }, new CollectionData { TagName = "SubChapter", ElementData = new List <WordTemplateData> { new WordTemplateData { new FieldData { TagName = "Change", Value = Guid.NewGuid().ToString() } } } } } } } }; const string template = "InputTemplate.docx"; const string output = "output.docx"; MemoryStream outputStream = null; Stream existingStream = null; try { using (var templateStream = File.Open(template, FileMode.Open)) { if (File.Exists(output)) { existingStream = File.Open(output, FileMode.Open); } outputStream = WordTemplate.Generate(data, templateStream, existingStream); } if (existingStream != null) { existingStream.Close(); } using (var outputWrite = new FileStream(output, FileMode.Create)) { outputStream.WriteTo(outputWrite); outputStream.Close(); } } finally { if (outputStream != null) { outputStream.Close(); } if (existingStream != null) { existingStream.Close(); } } }
public WordReport(WordTemplate template) : base(template) { }