Word Template Use

With GemBox.Document you can easily create a 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.

Screenshot

Template Use Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using System.Linq;
3using System.Text;
4using GemBox.Document;
5using GemBox.Document.Tables;
6using System.Text.RegularExpressions;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
15
16        DocumentModel document = DocumentModel.Load("Invoice.docx");
17
18        int numberOfItems = 10;
19
20        // Document contains 4 tables. Each table contains some set of information.
21        Table[] tables = document.GetChildElements(true, ElementType.Table).Cast<Table>().ToArray();
22
23        // First table contains invoice number and date.
24        Table invoiceTable = tables[0];
25        int invoiceNumber = 14;
26        // We can get and cast first paragraph using:
27        // a) Linq
28        invoiceTable.Rows[0].Cells[1].Blocks.Cast<Paragraph>().First().Inlines.Add(new Run(document, invoiceNumber.ToString()));
29        // b) ElementCollection.Cast<TElement>(int index)
30        invoiceTable.Rows[1].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, DateTime.Now.ToShortDateString()));
31
32        // Second table contains customer data.
33        Table customerTable = tables[1];
34        customerTable.Rows[0].Cells[1].Blocks.Add(new Paragraph(document, "ACME Corp"));
35        customerTable.Rows[1].Cells[1].Blocks.Add(new Paragraph(document, "240 Old Country Road, Springfield, IL"));
36        customerTable.Rows[2].Cells[1].Blocks.Add(new Paragraph(document, "USA"));
37        customerTable.Rows[3].Cells[1].Blocks.Add(new Paragraph(document, "Joe Smith"));
38
39        // Third table contains amount and prices.
40        Table mainTable = tables[2];
41        // Our main table contains only one row (for one item). If we have more items then we need to clone it.
42        for (int i = 1; i < numberOfItems; i++)
43            mainTable.Rows.Insert(1, mainTable.Rows[1].Clone(true));
44
45        DateTime startDate = DateTime.Now.AddDays(-numberOfItems);
46        int total = 0;
47
48        int rowIndex;
49        for (rowIndex = 1; rowIndex < numberOfItems + 1; rowIndex++)
50        {
51            mainTable.Rows[rowIndex].Cells[0].Blocks.Add(new Paragraph(document, startDate.AddDays(rowIndex - 1).ToString("dddd, MMMM d, yyyy")));
52            // We worked between 6 and 8 hours per day.
53            int workHours = rowIndex % 3 + 6;
54            int price = workHours * 35;
55            total += price;
56            mainTable.Rows[rowIndex].Cells[1].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, workHours.ToString()));
57            mainTable.Rows[rowIndex].Cells[2].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, "35.00"));
58            mainTable.Rows[rowIndex].Cells[3].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, price.ToString("0.00")));
59        }
60
61        mainTable.Rows[rowIndex].Cells[3].Blocks.Cast<Paragraph>(0).Inlines.Add(new Run(document, total.ToString("0.00")));
62
63        // Fourth table contains notes
64        Table notesTable = tables[3];
65        notesTable.Rows[1].Cells[0].Blocks.Add(new Paragraph(document, "Payment via check."));
66
67        document.Save("Template Use.docx");
68    }
69}
1Imports System
2Imports System.Linq
3Imports System.Text
4Imports GemBox.Document
5Imports GemBox.Document.Tables
6Imports System.Text.RegularExpressions
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Dim document As DocumentModel = DocumentModel.Load("Invoice.docx")
16
17        Dim numberOfItems As Integer = 10
18
19        ' Document contains 4 tables. Each table contains some set of information.
20        Dim tables As Table() = document.GetChildElements(True, ElementType.Table).Cast(Of Table)().ToArray()
21
22        ' First table contains invoice number and date.
23        Dim invoiceTable As Table = tables(0)
24        Dim invoiceNumber As Integer = 14
25        ' We can get and cast first paragraph using:
26        ' a) Linq
27        invoiceTable.Rows(0).Cells(1).Blocks.Cast(Of Paragraph)().First().Inlines.Add(New Run(document, invoiceNumber.ToString()))
28        ' b) ElementCollection.Cast<TElement>(int index)
29        invoiceTable.Rows(1).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, DateTime.Now.ToShortDateString()))
30
31        ' Second table contains customer data.
32        Dim customerTable As Table = tables(1)
33        customerTable.Rows(0).Cells(1).Blocks.Add(New Paragraph(document, "ACME Corp"))
34        customerTable.Rows(1).Cells(1).Blocks.Add(New Paragraph(document, "240 Old Country Road, Springfield, IL"))
35        customerTable.Rows(2).Cells(1).Blocks.Add(New Paragraph(document, "USA"))
36        customerTable.Rows(3).Cells(1).Blocks.Add(New Paragraph(document, "Joe Smith"))
37
38        ' Third table contains amount and prices.
39        Dim mainTable As Table = tables(2)
40        ' Our main table contains only one row (for one item). If we have more items then we need to clone it.
41        For i As Integer = 1 To numberOfItems - 1
42            mainTable.Rows.Insert(1, mainTable.Rows(1).Clone(True))
43        Next
44
45        Dim startDate As DateTime = DateTime.Now.AddDays(-numberOfItems)
46        Dim total As Integer = 0
47
48        Dim rowIndex As Integer
49        For rowIndex = 1 To numberOfItems
50            mainTable.Rows(rowIndex).Cells(0).Blocks.Add(New Paragraph(document, startDate.AddDays(rowIndex - 1).ToString("dddd, MMMM d, yyyy")))
51            ' We worked between 6 and 8 hours per day.
52            Dim workHours As Integer = rowIndex Mod 3 + 6
53            Dim price As Integer = workHours * 35
54            total += price
55            mainTable.Rows(rowIndex).Cells(1).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, workHours.ToString()))
56            mainTable.Rows(rowIndex).Cells(2).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, "35.00"))
57            mainTable.Rows(rowIndex).Cells(3).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, price.ToString("0.00")))
58        Next
59
60        mainTable.Rows(rowIndex).Cells(3).Blocks.Cast(Of Paragraph)(0).Inlines.Add(New Run(document, total.ToString("0.00")))
61
62        ' Fourth table contains notes
63        Dim notesTable As Table = tables(3)
64        notesTable.Rows(1).Cells(0).Blocks.Add(New Paragraph(document, "Payment via check."))
65
66        document.Save("Template Use.docx")
67
68    End Sub
69
70End Module

Check next sample or find out more about GemBox.Document and GemBox Software.