XingTemplateTags.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;
using GrapeCity.Documents.Word.Layout;
using GrapeCity.Documents.Pdf.Annotations;

namespace DsWordWeb.Demos
{
    // This sample demonstrates the DsWord Data/Report Templates API,
    // which allows developers to retrieve all template tags from a document,
    // along with details such as tag text, associated data element (if any),
    // and the tag's location within the document.
    //
    // In this sample, we retrieve all tags and replace each with a sequence of 'x' characters.
    //
    // See also: MarkTagsInPDF for an example of using this API in PDF export scenarios.
    public class XingTemplateTags
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();
            doc.Load(Path.Combine("Resources", "WordDocs", "House_Rental_Template.docx"));
            // Fetch information about all data template tags in the document:
            var tagInfos = doc.DataTemplate.GetTemplateTagInfos();
            // Replace all tags with sequences of 'X' character:
            foreach (GrapeCity.Documents.Word.Templates.TemplateTagInfo tagInfo in tagInfos)
            {
                var tagText = tagInfo.Text;
                tagInfo.Range.Replace(tagText, new string('x', tagText.Length));
            }
            return doc;
        }
    }
}