private static void createMsWordDocOrPdfFromMsWordDoc( MergeRowTree rowTree, bool ensureAllFieldsHaveValues, Stream inputStream, Stream destinationStream, bool saveAsMsWordDoc) { var doc = new Aspose.Words.Document(inputStream); // This is a hack we need to do because Aspose changed MailMerge.Execute to only support a single level of data. Since we support multiple levels, i.e. // child data, we need to use MailMerge.ExecuteWithRegions, which associates the specified enumerator with the top level "table" in the document instead // of the document itself. See http://www.aspose.com/community/forums/thread/315734.aspx. var builder = new Aspose.Words.DocumentBuilder(doc); builder.MoveToDocumentStart(); builder.InsertField("MERGEFIELD TableStart:Main"); builder.MoveToDocumentEnd(); builder.InsertField("MERGEFIELD TableEnd:Main"); doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions; doc.MailMerge.FieldMergingCallback = new ImageFieldMergingCallBack(); try { doc.MailMerge.ExecuteWithRegions(new AsposeMergeRowEnumerator("Main", rowTree.Rows, ensureAllFieldsHaveValues)); } catch (InvalidOperationException e) { // Aspose throws InvalidOperationException when there are problems with the template, such as a badly-formed region. throw new MailMergingException(e.Message); } doc.Save(destinationStream, saveAsMsWordDoc ? Aspose.Words.SaveFormat.Docx : Aspose.Words.SaveFormat.Pdf); }
private static void createMsWordDocOrPdfFromMsWordDoc( MergeRowTree rowTree, bool ensureAllFieldsHaveValues, Stream inputStream, Stream destinationStream, bool saveAsMsWordDoc) { var doc = new Aspose.Words.Document( inputStream ); // This is a hack we need to do because Aspose changed MailMerge.Execute to only support a single level of data. Since we support multiple levels, i.e. // child data, we need to use MailMerge.ExecuteWithRegions, which associates the specified enumerator with the top level "table" in the document instead // of the document itself. See http://www.aspose.com/community/forums/thread/315734.aspx. var builder = new Aspose.Words.DocumentBuilder( doc ); builder.MoveToDocumentStart(); builder.InsertField( "MERGEFIELD TableStart:Main" ); builder.MoveToDocumentEnd(); builder.InsertField( "MERGEFIELD TableEnd:Main" ); doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions; doc.MailMerge.FieldMergingCallback = new ImageFieldMergingCallBack(); try { doc.MailMerge.ExecuteWithRegions( new AsposeMergeRowEnumerator( "Main", rowTree.Rows, ensureAllFieldsHaveValues ) ); } catch( InvalidOperationException e ) { // Aspose throws InvalidOperationException when there are problems with the template, such as a badly-formed region. throw new MailMergingException( e.Message ); } doc.Save( destinationStream, saveAsMsWordDoc ? Aspose.Words.SaveFormat.Docx : Aspose.Words.SaveFormat.Pdf ); }