//
// 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 BatchStepProcessor class,
// which allows you to execute a single step of batch processing,
// processing a single specified record of the data source.
//
// See also the DataTplBatchOceans sample, which uses the same data source
// but processes all data items in a batch.
public class DataTplBatchStep
{
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);
// Initialize the batch step processor:
var batchProcessor = doc.DataTemplate.InitBatchStepProcessor(CultureInfo.GetCultureInfo("en-US"));
// Get a random record index to process:
var recordIndex = Random.Shared.Next(0, batchProcessor.MaxRecordIndex);
// Process the specific record index only.
// Note that we must save the document using the action passed to ProcessRecord(),
// as the original document reverts to its initial state after processing:
var ms = new MemoryStream();
batchProcessor.ProcessRecord(recordIndex, () => doc.Save(ms));
// Load the processed document from the memory stream and return it:
ms.Position = 0;
doc.Load(ms);
doc.Body.Paragraphs.Insert($"Note: This document was generated by processing record index {recordIndex} only.",
doc.Styles[BuiltInStyleId.BlockText], InsertLocation.Start);
return doc;
}
}
}