I'm trying to call
MailMerge.ExecuteNestedGroup on a
WordDocument with data that is deserialized from JSON (using Json.Net).
Here is a sample code which demonstrates the issue :
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
public static class Prog
{
public static void Main(string[] args)
{
// Dynamic
dynamic modelDynamic = new
{
IndividualName = "Bob",
IndividualAddress = new
{
City = "Montreal"
}
};
MergeWordAndModelIntoPdf(modelDynamic); // Ok
// Dictionary
var modelDictionary = new Dictionary<string, object>
{
{"IndividualName", "Bob Daule"},
{"IndividualAddress", new Dictionary<string,object>{{"City","Montreal"}}}
};
MergeWordAndModelIntoPdf(modelDictionary); // Fails
// Expando Object
dynamic modelExpandoObject = new ExpandoObject();
modelExpandoObject.IndividualName = "Bob";
modelExpandoObject.IndividualAddress = new ExpandoObject();
modelExpandoObject.IndividualAddress.City = "Montreal";
MergeWordAndModelIntoPdf(modelExpandoObject); // Fails
Console.ReadLine();
}
private static void MergeWordAndModelIntoPdf(object model)
{
const string InputDocument = "Order2.docx";
const string OutputDocument = "out.pdf";
using (var wordDoc = new WordDocument(File.OpenRead(InputDocument)))
{
wordDoc.MailMerge.ExecuteNestedGroup(new MailMergeDataTable("Model", new[] { model }));
using (var converter = new DocToPDFConverter())
{
using (var pdfDoc = converter.ConvertToPDF(wordDoc))
{
pdfDoc.Save(OutputDocument);
}
}
Process.Start(OutputDocument);
}
}
}
And my Word document is like this:
«BeginGroup:IndividualAddress»
«EndGroup:IndividualAddress»
When I use modelDynamic it works but I could not find a way to deserializing into a similar object which works...
When I use modelDictionary I get the following exception:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Syncfusion.DocIO.Base
StackTrace:
at Syncfusion.DocIO.DLS.MailMerge.GetFieldValue(String fieldName, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.GetFieldValue(IWMergeField field, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.UpdateMergeFieldValue(WMergeField mergeField, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroupForSelection(WTextBody textBody, Int32 itemStart, Int32 itemEnd, Int32 pItemStart, Int32 pItemEnd, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.OnBodyGroupFound(IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.CheckItem(ParagraphItem item)
at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.FindInBodyItems(BodyItemCollection bodyItems)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroupForSelection(WTextBody textBody, Int32 itemStart, Int32 itemEnd, Int32 pItemStart, Int32 pItemEnd, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.OnBodyGroupFound(IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.CheckItem(ParagraphItem item)
at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.FindInBodyItems(BodyItemCollection bodyItems)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroup(WSection section, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(String tableName)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(MailMergeDataTable dataTable)
at ConsoleApplication3.Prog.MergeWordAndModelIntoPdf(Object model) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 56
at ConsoleApplication3.Prog.Main(String[] args) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 36
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
When I use modelExpandoObject I get the following exception:
System.NotSupportedException was unhandled
HResult=-2146233067
Message=Specified method is not supported.
Source=System.Core
StackTrace:
at System.Dynamic.ExpandoObject.<GetExpandoEnumerator>d__46.System.Collections.IEnumerator.Reset()
at Syncfusion.DocIO.DLS.DataTableEnumerator..ctor(MailMergeDataTable table)
at Syncfusion.DocIO.DLS.MailMerge.GetEnum(String tableName)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroupForSelection(WTextBody textBody, Int32 itemStart, Int32 itemEnd, Int32 pItemStart, Int32 pItemEnd, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.OnBodyGroupFound(IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.CheckItem(ParagraphItem item)
at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.FindInBodyItems(BodyItemCollection bodyItems)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroup(WSection section, IRowsEnumerator rowsEnum)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(String tableName)
at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(MailMergeDataTable dataTable)
at ConsoleApplication3.Prog.MergeWordAndModelIntoPdf(Object model) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 56
at CallSite.Target(Closure , CallSite , Type , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
at ConsoleApplication3.Prog.Main(String[] args) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 44
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: