Hello,
here is our code for generating documents:
public class BobTheBuilder
{
public void GenerateReport(byte[] inputTemplate, string outputDocument, object data, SaveOptions saveOption)
{
using (var inputTemplateStream = new MemoryStream(inputTemplate))
using (var tmpDocumentStream = new MemoryStream())
using (var outputDocumentStream = new FileStream(outputDocument, FileMode.Create))
{
this.GeneratePdfReport(inputTemplateStream, tmpDocumentStream, data, saveOption);
var document = Document.Load(tmpDocumentStream);
document.Save(outputDocumentStream, saveOption);
}
}
private void GeneratePdfReport(Stream template, Stream report, object data, SaveOptions saveOption)
{
var documentGenerator = new DocumentGenerator(data);
documentGenerator.ElementInitialize += this.DocumentGeneratorOnElementInitialize;
var result = documentGenerator.GenerateDocument(template, report, saveOption);
if (!result.HasErrors) return;
foreach (var error in result.Errors)
{
Console.WriteLine(error);
}
}
private void DocumentGeneratorOnElementInitialize(object sender, ElementInitializeEventArgs elementInitializeEventArgs)
{
}
}
The suggested workaround to update the Table of Contents in the document is to load the generated Document and Save it using the Document.Load() and Document.Save().
However this line:
var document = Document.Load(tmpDocumentStream);
causes the following exception: (formatting is a bit weird, sorry about that)
System.Exception was unhandled by user code HResult=-2146233088
Message=Style 'Normal' cannot be added to the document because a style with the same name already exists in the document.
Source=Docentric.Documents.ObjectModel StackTrace: at Docentric.Documents.ObjectModel.StyleList.InsertItem(Int32 index, Style item) at System.Collections.ObjectModel.Collection`1.Add(T item) at #=quEhVhX4UgtrL14nHWlNtfPKvADhaDW2LOIq2fqTCiHIgs5OhP3gf9LUPx$JaKmDVirVIqgzQZ90tsL7EGrDw==.#=qnhtwKI2KtSdxWYaiT$oitnK$Mm_bDD18nHX$Iq3$P78=(Style=q6mojHLSkT2IP7xZaBAkawQ==, Style #=q7d4hLBnXmk2npWnacH5OOA==, Dictionary`2 #=q17Vohh$PVEjIiHiQUKSxuAOtrkhNZjXUCAGioZMVOUE=)
at #=quEhVhX4UgtrL14nHWlNtfPKv_ADhaDW2LOIq2fqTCiHIgs5OhP3gf_9LUPx$JaKmDVirVIqgzQZ90tsL7EGrDw==.#=qMQ6_gR7dOi2J00gufex$dg==() at #=quEhVhX4UgtrL14nHWlNtfPKv_ADhaDW2LOIq2fqTCiHIgs5OhP3gf_9LUPx$JaKmDVirVIqgzQZ90tsL7EGrDw==.#=q2wDK3LTq5B4xt2FWBJecEA==(Stream #=qwoGEvXFmcI4PREI2uWPNEQ==) at Docentric.Documents.ObjectModel.Document.#=qqX8gJaBN$QNzpEVh9$xS$w==(Stream
=qo5AcTlWDQuACz7z9Lbuy2A==)
at Docentric.Documents.ObjectModel.Document.Load(Stream stream) at WindwardPrototype.ViewModel.BobTheBuilder.GenerateReport(Byte[]
inputTemplate, String outputDocument, Object data, SaveOptions saveOption) in c:\Users\buttlaj\Documents\Visual Studio 2013\Projects\WindwardPrototype\WindwardPrototype\ViewModel\BobTheBuilder.cs:line 18 at WindwardPrototype.ViewModel.MainViewModel.GenerateDocument() in c:\Users\buttlaj\Documents\Visual Studio 2013\Projects\WindwardPrototype\WindwardPrototype\ViewModel\MainViewModel.cs:line 132 InnerException: