//
// 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.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 Layout API that allows developers during export
// to PDF to locate in the exported document the Data Template tags and to highlight
// them or process in some other way. Note that this sample does not bind the template
// to data (i.e. it does not call the GcWordDocument.DataTemplate.Process() method),
// it only highlights the tags when the template document is exported to PDF.
// This functionality can be used for example to review the templates prior to further processing.
// To use the API when exporting the DOCX:
// - Set MarkTemplateTagAreas to true in WordLayoutSettings, and
// - Specify your custom MarkupTemplateTags method in PdfOutputSettings.
//
// See also: XingTemplateTags for an example of using this API to modify the tags in the original DOCX.
public class MarkTagsInPDF
{
public GcWordDocument CreateDocx()
{
var doc = new GcWordDocument();
doc.Load(Path.Combine("Resources", "WordDocs", "House_Rental_Template.docx"));
return doc;
}
// Custom Word Layout settings set the MarkTemplateTagAreas property set to true,
// this makes the layout engine mark locations of Report/Data Template tags
// in the layout result, so that the tags can be for example highlighted
// for custom processing.
public static WordLayoutSettings GetWordLayoutSettings()
{
return new WordLayoutSettings()
{
MarkTemplateTagAreas = true
};
}
// Custom PDF output settings include a MarkupTemplateTags method
// that adds markup annotations over Report/Data Template tags
// present in the original DOCX. For this to work, the WordLayoutSettings
// (see above) must have the MarkTemplateTagAreas property set to true
// (it is false by default).
public static PdfOutputSettings GetPdfOutputSettings()
{
var settings = new PdfOutputSettings()
{
MarkupTemplateTags = (page, tags) =>
{
foreach (var tag in tags)
{
foreach (var rect in tag.Areas)
{
var hilight = new TextMarkupAnnotation
{
Area = [rect],
Color = Color.YellowGreen,
MarkupType = TextMarkupType.Highlight,
Opacity = 0.4f,
RichText = $"Tag Info:<br>Name: <b>{tag?.Info?.Name}</b><br>Text: <b>{tag?.Info?.Text}</b>",
};
page.Annotations.Add(hilight);
}
}
}
};
return settings;
}
}
}