DataTplImage.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.Collections.Generic;
- using System.Linq;
- using System.Globalization;
- using GrapeCity.Documents.Word;
-
- namespace DsWordWeb.Demos
- {
- // This sample shows how to use the image formatter in a data template.
- public class DataTplImage
- {
-
- public GcWordDocument CreateDocx()
- {
- // The data source.
- //
- // The 'imagePath' yields the absolute path of an image,
- // which is one of accepted value types for the image formatter.
- //
- // For reference, the following value types are supported by the image formatter:
- // - A byte array containing the image data;
- // - A System.IO.Stream object that can be used to read the image data;
- // - A System.Drawing.Image object;
- // - A string from which an absolute file URI of the image can be created, or Base64-encoded image data.
- var data = new[]
- {
- new { name = "Minerva", imagePath = Path.GetFullPath(Path.Combine("Resources", "Images", "minerva.jpg")) },
- new { name = "Colosseum", imagePath = Path.GetFullPath(Path.Combine("Resources", "Images", "colosseum.jpg")) },
- new { name = "Venus Felix", imagePath = Path.GetFullPath(Path.Combine("Resources", "Images", "lady.jpg")) },
- };
-
- var doc = new GcWordDocument();
-
- // Add the data source to the data template data sources:
- doc.DataTemplate.DataSources.Add("ds", data);
-
- // The single paragraph added to the document contains template tags:
- // - {{#ds}}..{{/ds}} -- root template, 'ds' is the name of the data source;
- // - {{ds.name}} -- fetches the image name;
- // - {{ds.imagePath}:image(266,400)} -- fetches the image file, resizes the image to specified size.
- var p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.name}}:\n{{ds.imagePath}:image(266,400)}{{/ds}}");
-
- // This call expands all data templates in the document,
- // replacing template tags with data (iterating over all data items):
- doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
-
- // Done:
- return doc;
- }
- }
- }
-