//
// 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 demonstrates the use of DataTemplate.BatchProcess() method,
// which allows to loop over the root items of a data source, generating
// a separate DOCX for each data source item.
public class DataTplBatchOceans
{
public GcWordDocument CreateDocx()
{
// The data source (ocean and sea data from Wikipedia):
var oceans = new[]
{
new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
{ new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"} }
},
new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
{ new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
},
new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
{ new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
},
new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
{ new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
},
new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
{ new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
},
};
// Create and load the template DOCX:
var doc = new GcWordDocument();
doc.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"));
// Add the data source to the data template data sources
doc.DataTemplate.DataSources.Add("ds", oceans);
// In this sample, we use the DataTemplate.BatchProcess() method which iterates over the root data items,
// generating a separate document for each item. To allow the sample to show the result as a single document,
// we add each generated document as a separate section to a new DOCX.
// Note that when DataTemplate.BatchProcess() finishes processing, the original GcWordDocument on which
// it was called reverts to its initial state (i.e. it contains the unprocessed template tags rather than data).
// If you want to preserve the results of the batch processing, you must save the document in its current
// state each time the 'itemProcessed' action gets called.
// The document to contain all results:
var allDocs = new GcWordDocument();
// This makes sure the resulting document has the same styles as the template:
allDocs.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx"));
allDocs.Body.Clear();
// The extra section break:
Section lastSection = null;
// Batch process the template:
doc.DataTemplate.BatchProcess(itemProcessed, CultureInfo.GetCultureInfo("en-US"));
// Delete the extra section break:
lastSection.Delete();
// Done:
return allDocs;
// The callback method called as each root data item is processed:
void itemProcessed()
{
// In order to keep the processed template, we must either save the original document
// in its current state to a file, or copy its content to another document as we do here,
// as the content is regenerated for each root data item, and is reset to the original
// template when processing completes:
doc.Body.CopyTo(allDocs.Body, InsertLocation.End, FormattingCopyStrategy.Copy);
lastSection = allDocs.Body.Paragraphs.Last.AddSectionBreak();
}
}
}
}