ReplaceInTemplate.cs
- //
- // This code is part of Document Solutions for Word demos.
- // Copyright (c) MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using System.Linq;
- using GrapeCity.Documents.Word;
-
- namespace DsWordWeb.Demos
- {
- // This sample shows how to fill a template document with data
- // using the RangeBase.Replace extension method provided by
- // the RangeBaseFindReplaceExtensions class.
- //
- // The template document is similar to the document generated by
- // the ProcurementLetter sample, but the data values in that document
- // were changed to template placeholders (in the form '__%<name>%__')
- // using MS Word. In the sample, we use the text replace functionality
- // to change the placeholders back to original data. The resulting
- // document can be viewed by running the ProcurementLetterTpl sample.
- //
- // In a real life application, the same approach can be used for example
- // to go over a record set, generating a document for each record.
- public class ReplaceInTemplate
- {
- public GcWordDocument CreateDocx()
- {
- // Placeholders and sample data:
- (string placeholder, string data)[] placeholderData =
- {
- ("__%c_name%__", "Nancy Davolio"),
- ("__%c_title%__", "Chief Procurement Officer"),
- ("__%c_phone%__", "555-543-5432"),
- ("__%c_url%__", "www.acmeinc.com"),
- ("__%c_address%__", "5432 Street West, Townsvilla, State 54321"),
- ("__%to_name%__", "Mark"),
- ("__%dlr_name%__", "AMA Ltd"),
- ("__%order_no%__", "8393"),
- };
-
- // Load the document:
- var doc = new GcWordDocument();
- doc.Load(Path.Combine("Resources", "WordDocs", "ProcurementLetterTpl.docx"));
-
- // Replace all placeholders in the body with sample data values:
- foreach ((string placeholder, string data) pair in placeholderData)
- doc.Body.Replace(pair.placeholder, pair.data);
-
- // Done:
- return doc;
- }
- }
- }
-