From fe2e4ef395e1497bd50c2a3ac83d80244d84d536 Mon Sep 17 00:00:00 2001 From: vderyushev Date: Thu, 11 Jul 2024 11:06:05 +0300 Subject: [PATCH] Updated examples --- .../Base operations.cs | 29 ++++++++++----- .../Apply custom logic to empty regions.cs | 1 + .../Create MailMerge template.cs | 6 ++-- .../Nested MailMerge custom.cs | 5 +-- .../Working with CleanupOptions.cs | 7 +++- .../Working with Fields.cs | 35 +++++++++++++++++-- .../Working with XML data.cs | 2 ++ .../Working with Fields.cs | 3 ++ 8 files changed, 73 insertions(+), 15 deletions(-) diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs index d2dccaca3..03f87157a 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs @@ -7,12 +7,13 @@ namespace DocsExamples.Mail_Merge_and_Reporting { - internal class BaseOperations : DocsExamplesBase + public class BaseOperations : DocsExamplesBase { [Test] public void SimpleMailMerge() { - //ExStart:SimpleMailMerge + //ExStart:ExecuteSimpleMailMerge + //GistId:341b834e9b6a84ac6885e907e0ea4229 // Include the code for our template. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); @@ -29,26 +30,28 @@ public void SimpleMailMerge() new object[] { "John Doe", "Hawaiian", "2" }); doc.Save(ArtifactsDir + "BaseOperations.SimpleMailMerge.docx"); - //ExEnd:SimpleMailMerge + //ExEnd:ExecuteSimpleMailMerge } [Test] public void UseIfElseMustache() { - //ExStart:UseOfifelseMustacheSyntax + //ExStart:UseIfElseMustache + //GistId:544788f602e697802e313a641cedb9b8 Document doc = new Document(MyDir + "Mail merge destinations - Mustache syntax.docx"); doc.MailMerge.UseNonMergeFields = true; doc.MailMerge.Execute(new[] { "GENDER" }, new object[] { "MALE" }); doc.Save(ArtifactsDir + "BaseOperations.IfElseMustache.docx"); - //ExEnd:UseOfifelseMustacheSyntax + //ExEnd:UseIfElseMustache } [Test] public void MustacheSyntaxUsingDataTable() { //ExStart:MustacheSyntaxUsingDataTable + //GistId:544788f602e697802e313a641cedb9b8 Document doc = new Document(MyDir + "Mail merge destinations - Vendor.docx"); // Loop through each row and fill it with data. @@ -75,6 +78,7 @@ public void MustacheSyntaxUsingDataTable() public void ExecuteWithRegionsDataTable() { //ExStart:ExecuteWithRegionsDataTable + //GistId:de5e13f5d5bb7d8cb88da900b4f9ed8b Document doc = new Document(MyDir + "Mail merge destinations - Orders.docx"); // Use DataTable as a data source. @@ -137,6 +141,7 @@ private DataTable ExecuteDataTable(string commandText) public void ProduceMultipleDocuments() { //ExStart:ProduceMultipleDocuments + //GistId:341b834e9b6a84ac6885e907e0ea4229 string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DatabaseDir + "Northwind.accdb"; Document doc = new Document(MyDir + "Mail merge destination - Northwind suppliers.docx"); @@ -168,10 +173,11 @@ public void ProduceMultipleDocuments() } #endif - //ExStart:MailMergeWithRegions [Test] public void MailMergeWithRegions() { + //ExStart:MailMergeWithRegions + //GistId:341b834e9b6a84ac6885e907e0ea4229 Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); @@ -204,15 +210,16 @@ public void MailMergeWithRegions() // The end point of mail merge with regions. builder.InsertField(" MERGEFIELD TableEnd:Customers"); - // Pass our dataset to perform mail merge with regions. + // Pass our dataset to perform mail merge with regions. DataSet customersAndOrders = CreateDataSet(); doc.MailMerge.ExecuteWithRegions(customersAndOrders); doc.Save(ArtifactsDir + "BaseOperations.MailMergeWithRegions.docx"); + //ExEnd:MailMergeWithRegions } - //ExEnd:MailMergeWithRegions //ExStart:CreateDataSet + //GistId:341b834e9b6a84ac6885e907e0ea4229 private DataSet CreateDataSet() { // Create the customers table. @@ -247,8 +254,14 @@ private DataSet CreateDataSet() public void GetRegionsByName() { //ExStart:GetRegionsByName + //GistId:b4bab1bf22437a86d8062e91cf154494 Document doc = new Document(MyDir + "Mail merge regions.docx"); + //ExStart:GetRegionsHierarchy + //GistId:b4bab1bf22437a86d8062e91cf154494 + MailMergeRegionInfo regionInfo = doc.MailMerge.GetRegionsHierarchy(); + //ExEnd:GetRegionsHierarchy + IList regions = doc.MailMerge.GetRegionsByName("Region1"); Assert.AreEqual(1, doc.MailMerge.GetRegionsByName("Region1").Count); foreach (MailMergeRegionInfo region in regions) Assert.AreEqual("Region1", region.Name); diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Apply custom logic to empty regions.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Apply custom logic to empty regions.cs index b2e788ec8..46e69e8f6 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Apply custom logic to empty regions.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Apply custom logic to empty regions.cs @@ -303,6 +303,7 @@ private DataSet GetDataSource() private void DisableForeignKeyConstraints(DataSet dataSet) { //ExStart:DisableForeignKeyConstraints + //GistId:c68048adceb3bda6a1511c7d6f5ebf7b dataSet.Relations.Add(new DataRelation("OrderToItem", orderTable.Columns["Order_Id"], itemTable.Columns["Order_Id"], false)); //ExEnd:DisableForeignKeyConstraints diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Create MailMerge template.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Create MailMerge template.cs index d56aef8b5..fd49f1eaa 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Create MailMerge template.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Create MailMerge template.cs @@ -5,9 +5,11 @@ namespace DocsExamples.Mail_Merge_and_Reporting.Custom_examples { internal class CreateMailMergeTemplate { - //ExStart:CreateMailMergeTemplate + public Document Template() { + //ExStart:CreateMailMergeTemplate + //GistId:0a1baaa127443b485cc692c8d98ee353 DocumentBuilder builder = new DocumentBuilder(); // Insert a text input field the unique name of this field is "Hello", the other parameters define @@ -51,7 +53,7 @@ public Document Template() builder.InsertField(@"MERGEFIELD EmployeeDepartment \* MERGEFORMAT"); return builder.Document; + //ExEnd:CreateMailMergeTemplate } - //ExEnd:CreateMailMergeTemplate } } diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Nested MailMerge custom.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Nested MailMerge custom.cs index 9703e4423..da5d49a02 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Nested MailMerge custom.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Complex examples and helpers/Nested MailMerge custom.cs @@ -137,7 +137,8 @@ public bool MoveNext() return !IsEof; } - //ExStart:GetChildDataSourceExample + //ExStart:GetChildDataSource + //GistId:c68048adceb3bda6a1511c7d6f5ebf7b public IMailMergeDataSource GetChildDataSource(string tableName) { switch (tableName) @@ -149,7 +150,7 @@ public IMailMergeDataSource GetChildDataSource(string tableName) return null; } } - //ExEnd:GetChildDataSourceExample + //ExEnd:GetChildDataSource private bool IsEof => (mRecordIndex >= mCustomers.Count); diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with CleanupOptions.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with CleanupOptions.cs index 6ecf34d91..599e74526 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with CleanupOptions.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with CleanupOptions.cs @@ -56,6 +56,7 @@ public void CleanupParagraphsWithPunctuationMarks() public void RemoveUnmergedRegions() { //ExStart:RemoveUnmergedRegions + //GistId:f39874821cb317d245a769c9ce346fea Document doc = new Document(MyDir + "Mail merge destination - Northwind suppliers.docx"); DataSet data = new DataSet(); @@ -63,7 +64,7 @@ public void RemoveUnmergedRegions() doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions; // doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields; // doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveStaticFields; - // doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs; + // doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs; // doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields; //ExEnd:MailMergeCleanupOptions @@ -79,6 +80,7 @@ public void RemoveUnmergedRegions() public void RemoveEmptyParagraphs() { //ExStart:RemoveEmptyParagraphs + //GistId:f39874821cb317d245a769c9ce346fea Document doc = new Document(MyDir + "Table with fields.docx"); doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs; @@ -94,6 +96,7 @@ public void RemoveEmptyParagraphs() public void RemoveUnusedFields() { //ExStart:RemoveUnusedFields + //GistId:f39874821cb317d245a769c9ce346fea Document doc = new Document(MyDir + "Table with fields.docx"); doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields; @@ -109,6 +112,7 @@ public void RemoveUnusedFields() public void RemoveContainingFields() { //ExStart:RemoveContainingFields + //GistId:f39874821cb317d245a769c9ce346fea Document doc = new Document(MyDir + "Table with fields.docx"); doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields; @@ -124,6 +128,7 @@ public void RemoveContainingFields() public void RemoveEmptyTableRows() { //ExStart:RemoveEmptyTableRows + //GistId:f39874821cb317d245a769c9ce346fea Document doc = new Document(MyDir + "Table with fields.docx"); doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyTableRows; diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with Fields.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with Fields.cs index 403f6fa47..eadb1be93 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with Fields.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with Fields.cs @@ -16,6 +16,7 @@ internal class WorkingWithFields : DocsExamplesBase public void MailMergeFormFields() { //ExStart:MailMergeFormFields + //GistId:0a1baaa127443b485cc692c8d98ee353 Document doc = new Document(MyDir + "Mail merge destinations - Fax.docx"); // Setup mail merge event handler to do the custom work. @@ -40,6 +41,7 @@ public void MailMergeFormFields() } //ExStart:HandleMergeField + //GistId:8a66b5cea0f9f8b862c092c9b93ccb3c private class HandleMergeField : IFieldMergingCallback { /// @@ -81,6 +83,7 @@ void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) } //ExStart:ImageFieldMerging + //GistId:0a1baaa127443b485cc692c8d98ee353 void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args) { args.ImageFileName = "Image.png"; @@ -96,7 +99,8 @@ void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args) [Test] public void MailMergeImageField() { - //ExStart:MailMergeImageField + //ExStart:MailMergeImageField + //GistId:8a66b5cea0f9f8b862c092c9b93ccb3c Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); @@ -120,6 +124,7 @@ public void MailMergeImageField() } //ExStart:ImageFieldMergingHandler + //GistId:8a66b5cea0f9f8b862c092c9b93ccb3c private class ImageFieldMergingHandler : IFieldMergingCallback { void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) @@ -142,6 +147,7 @@ void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args) //ExEnd:ImageFieldMergingHandler //ExStart:DataSourceRoot + //GistId:8a66b5cea0f9f8b862c092c9b93ccb3c public class DataSourceRoot : IMailMergeDataSourceRoot { public IMailMergeDataSource GetDataSource(string s) @@ -215,6 +221,7 @@ public void MailMergeAndConditionalField() public void MailMergeImageFromBlob() { //ExStart:MailMergeImageFromBlob + //GistId:8a66b5cea0f9f8b862c092c9b93ccb3c Document doc = new Document(MyDir + "Mail merge destination - Northwind employees.docx"); doc.MailMerge.FieldMergingCallback = new HandleMergeImageFieldFromBlob(); @@ -234,7 +241,8 @@ public void MailMergeImageFromBlob() //ExEnd:MailMergeImageFromBlob } - //ExStart:HandleMergeImageFieldFromBlob + //ExStart:HandleMergeImageFieldFromBlob + //GistId:8a66b5cea0f9f8b862c092c9b93ccb3c public class HandleMergeImageFieldFromBlob : IFieldMergingCallback { void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) @@ -386,5 +394,28 @@ private DataTable GetSuppliersDataTable() return dataTable; } //ExEnd:HandleMergeFieldAlternatingRows + + [Test] + public void FieldNext() + { + //ExStart:FieldNext + //GistId:b4bab1bf22437a86d8062e91cf154494 + Document doc = new Document(); + DocumentBuilder builder = new DocumentBuilder(doc); + + // Use NextIf field. A NEXTIF field has the same function as a NEXT field, + // but it skips to the next row only if a statement constructed by the following 3 properties is true. + FieldNextIf fieldNextIf = (FieldNextIf)builder.InsertField(FieldType.FieldNextIf, true); + + // Or use SkipIf field. + FieldNextIf fieldSkipIf = (FieldNextIf)builder.InsertField(FieldType.FieldSkipIf, true); + + fieldNextIf.LeftExpression = "5"; + fieldNextIf.RightExpression = "2 + 3"; + fieldNextIf.ComparisonOperator = "="; + + doc.Save(ArtifactsDir + "WorkingWithFields.FieldNext.docx"); + //ExEnd:FieldNext + } } } \ No newline at end of file diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with XML data.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with XML data.cs index fe02a54bd..ce3c0a239 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with XML data.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Working with XML data.cs @@ -16,6 +16,7 @@ internal class WorkingWithXmlData : DocsExamplesBase public void XmlMailMerge() { //ExStart:XmlMailMerge + //GistId:0441f68c5209fec25c47d1a0a203fbb0 DataSet customersDs = new DataSet(); customersDs.ReadXml(MyDir + "Mail merge data - Customers.xml"); @@ -30,6 +31,7 @@ public void XmlMailMerge() public void NestedMailMerge() { //ExStart:NestedMailMerge + //GistId:c68048adceb3bda6a1511c7d6f5ebf7b // The Datatable.TableNames and the DataSet.Relations are defined implicitly by .NET through ReadXml. DataSet pizzaDs = new DataSet(); pizzaDs.ReadXml(MyDir + "Mail merge data - Orders.xml"); diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs index ebda7ce9f..b8d308e67 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs @@ -427,6 +427,7 @@ public void InsertAdvanceFieldWithOutDocumentBuilder() public void GetMailMergeFieldNames() { //ExStart:GetFieldNames + //GistId:b4bab1bf22437a86d8062e91cf154494 Document doc = new Document(); string[] fieldNames = doc.MailMerge.GetFieldNames(); @@ -438,6 +439,7 @@ public void GetMailMergeFieldNames() public void MappedDataFields() { //ExStart:MappedDataFields + //GistId:b4bab1bf22437a86d8062e91cf154494 Document doc = new Document(); doc.MailMerge.MappedDataFields.Add("MyFieldName_InDocument", "MyFieldName_InDataSource"); @@ -448,6 +450,7 @@ public void MappedDataFields() public void DeleteFields() { //ExStart:DeleteFields + //GistId:f39874821cb317d245a769c9ce346fea Document doc = new Document(); doc.MailMerge.DeleteFields();