Following example can be found and executed in GemBox.Document Sample Explorer under category 'Basic Features' and name 'Template Use'.
Example
With GemBox.Document you can easily create new document from an existing template document. The procedure is simple: load a template document, fill it with your data and save it as a new document.
Following sample demonstrates how to generate an invoice from a template document by inserting data into document content. To avoid manual data insertion, use Mail Merge - a process of merging or importing data from a data source to a template document.
using System; using System.Linq; using System.Text; using GemBox.Document; using GemBox.Document.Tables; class Sample { [STAThread] static void Main(string[] args) { // If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); // If sample exceeds Free version limitations then continue as trial version. ComponentInfo.FreeLimitReached += (sender, e) => e.ContinueAsTrial = true; string fileName = @"C:\Users\User\Documents\SVN\Document\v1.0\DocumentCode\SampleExplorer\GBDSampleExplorer\Data\Invoice.docx"; DocumentModel document = DocumentModel.Load(fileName, DocxLoadOptions.DocxDefault); int numberOfItems = 10; // Document contains 4 tables. Each table contains some set of information. Table[] tables = document.GetChildElements(true, ElementType.Table).Cast<Table>().ToArray(); // First table contains invoice number and date. Table invoiceTable = tables[0]; int invoiceNumber = 14; // We can get and cast first paragraph using: // a) Linq invoiceTable.Rows[0].Cells[1].Blocks.Cast<Paragraph>().First().Inlines.Add(new Run(document, invoiceNumber.ToString())); // b) ElementCollection.Cast<TElement>(int index) invoiceTable.Rows[1].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, DateTime.Now.ToShortDateString())); // Second table contains customer data. Table customerTable = tables[1]; customerTable.Rows[0].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, "ACME Corp")); customerTable.Rows[1].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, "240 Old Country Road, Springfield, IL")); customerTable.Rows[2].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, "USA")); customerTable.Rows[3].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, "Joe Smith")); // Third table contains amount and prices. Table mainTable = tables[2]; // Our main table contains only one row (for one item). If we have more items then we need to clone it. for (int i = 1; i < numberOfItems; i++) mainTable.Rows.Insert(1, mainTable.Rows[1].Clone(true)); DateTime startDate = DateTime.Now.AddDays(-numberOfItems); int total = 0; int rowIndex; for (rowIndex = 1; rowIndex < numberOfItems + 1; rowIndex++) { mainTable.Rows[rowIndex].Cells[0].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, startDate.AddDays(rowIndex - 1).ToString("dddd, MMMM d, yyyy"))); // We worked between 6 and 8 hours per day. int workHours = rowIndex % 3 + 6; int price = workHours * 35; total += price; mainTable.Rows[rowIndex].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, workHours.ToString())); mainTable.Rows[rowIndex].Cells[2].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, "35.00")); mainTable.Rows[rowIndex].Cells[3].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, price.ToString("0.00"))); } mainTable.Rows[rowIndex].Cells[3].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, total.ToString("0.00"))); // Fourth table contains notes Table notesTable = tables[3]; notesTable.Rows[1].Cells[0].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, "Payment via check.")); string outPath = @"C:\Users\User\AppData\Local\GBDSampleExplorer\Template Use.docx"; document.Save(outPath, DocxSaveOptions.DocxDefault); } }
Imports System Imports System.Linq Imports System.Text Imports GemBox.Document Imports GemBox.Document.Tables Module Samples Sub Main() ' If using Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY") ' If sample exceeds Free version limitations then continue as trial version. AddHandler ComponentInfo.FreeLimitReached, Sub(sender, e) e.ContinueAsTrial = True Dim fileName As String = "C:\Users\User\Documents\SVN\Document\v1.0\DocumentCode\SampleExplorer\GBDSampleExplorer\Data\Invoice.docx" Dim document As DocumentModel = DocumentModel.Load(fileName, DocxLoadOptions.DocxDefault) Dim numberOfItems As Integer = 10 ' Document contains 4 tables. Each table contains some set of information. Dim tables As Table() = document.GetChildElements(True, ElementType.Table).Cast(Of Table)().ToArray() ' First table contains invoice number and date. Dim invoiceTable As Table = tables(0) Dim invoiceNumber As Integer = 14 ' We can get and cast first paragraph using: ' a) Linq invoiceTable.Rows(0).Cells(1).Blocks.Cast(Of Paragraph)().First().Inlines.Add(New Run(document, invoiceNumber.ToString())) ' b) ElementCollection.Cast<TElement>(int index) invoiceTable.Rows(1).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, DateTime.Now.ToShortDateString())) ' Second table contains customer data. Dim customerTable As Table = tables(1) customerTable.Rows(0).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, "ACME Corp")) customerTable.Rows(1).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, "240 Old Country Road, Springfield, IL")) customerTable.Rows(2).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, "USA")) customerTable.Rows(3).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, "Joe Smith")) ' Third table contains amount and prices. Dim mainTable As Table = tables(2) ' Our main table contains only one row (for one item). If we have more items then we need to clone it. For i As Integer = 1 To numberOfItems - 1 mainTable.Rows.Insert(1, mainTable.Rows(1).Clone(True)) Next Dim startDate As DateTime = DateTime.Now.AddDays(-numberOfItems) Dim total As Integer = 0 Dim rowIndex As Integer For rowIndex = 1 To numberOfItems mainTable.Rows(rowIndex).Cells(0).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, startDate.AddDays(rowIndex - 1).ToString("dddd, MMMM d, yyyy"))) ' We worked between 6 and 8 hours per day. Dim workHours As Integer = rowIndex Mod 3 + 6 Dim price As Integer = workHours * 35 total += price mainTable.Rows(rowIndex).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, workHours.ToString())) mainTable.Rows(rowIndex).Cells(2).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, "35.00")) mainTable.Rows(rowIndex).Cells(3).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, price.ToString("0.00"))) Next mainTable.Rows(rowIndex).Cells(3).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, total.ToString("0.00"))) ' Fourth table contains notes Dim notesTable As Table = tables(3) notesTable.Rows(1).Cells(0).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, "Payment via check.")) Dim outPath As String = "C:\Users\User\AppData\Local\GBDSampleExplorer\Template Use.docx" document.Save(outPath, DocxSaveOptions.DocxDefault) End Sub End Module