Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vderyushev committed Jul 11, 2024
1 parent 9dd4d51 commit fe2e4ef
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<MailMergeRegionInfo> regions = doc.MailMerge.GetRegionsByName("Region1");
Assert.AreEqual(1, doc.MailMerge.GetRegionsByName("Region1").Count);
foreach (MailMergeRegionInfo region in regions) Assert.AreEqual("Region1", region.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +53,7 @@ public Document Template()
builder.InsertField(@"MERGEFIELD EmployeeDepartment \* MERGEFORMAT");

return builder.Document;
//ExEnd:CreateMailMergeTemplate
}
//ExEnd:CreateMailMergeTemplate
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public bool MoveNext()
return !IsEof;
}

//ExStart:GetChildDataSourceExample
//ExStart:GetChildDataSource
//GistId:c68048adceb3bda6a1511c7d6f5ebf7b
public IMailMergeDataSource GetChildDataSource(string tableName)
{
switch (tableName)
Expand All @@ -149,7 +150,7 @@ public IMailMergeDataSource GetChildDataSource(string tableName)
return null;
}
}
//ExEnd:GetChildDataSourceExample
//ExEnd:GetChildDataSource

private bool IsEof => (mRecordIndex >= mCustomers.Count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ 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();
//ExStart:MailMergeCleanupOptions
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

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -40,6 +41,7 @@ public void MailMergeFormFields()
}

//ExStart:HandleMergeField
//GistId:8a66b5cea0f9f8b862c092c9b93ccb3c
private class HandleMergeField : IFieldMergingCallback
{
/// <summary>
Expand Down Expand Up @@ -81,6 +83,7 @@ void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
}

//ExStart:ImageFieldMerging
//GistId:0a1baaa127443b485cc692c8d98ee353
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
args.ImageFileName = "Image.png";
Expand All @@ -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);

Expand All @@ -120,6 +124,7 @@ public void MailMergeImageField()
}

//ExStart:ImageFieldMergingHandler
//GistId:8a66b5cea0f9f8b862c092c9b93ccb3c
private class ImageFieldMergingHandler : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
Expand All @@ -142,6 +147,7 @@ void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
//ExEnd:ImageFieldMergingHandler

//ExStart:DataSourceRoot
//GistId:8a66b5cea0f9f8b862c092c9b93ccb3c
public class DataSourceRoot : IMailMergeDataSourceRoot
{
public IMailMergeDataSource GetDataSource(string s)
Expand Down Expand Up @@ -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();
Expand All @@ -234,7 +241,8 @@ public void MailMergeImageFromBlob()
//ExEnd:MailMergeImageFromBlob
}

//ExStart:HandleMergeImageFieldFromBlob
//ExStart:HandleMergeImageFieldFromBlob
//GistId:8a66b5cea0f9f8b862c092c9b93ccb3c
public class HandleMergeImageFieldFromBlob : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
Expand Down Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ public void InsertAdvanceFieldWithOutDocumentBuilder()
public void GetMailMergeFieldNames()
{
//ExStart:GetFieldNames
//GistId:b4bab1bf22437a86d8062e91cf154494
Document doc = new Document();

string[] fieldNames = doc.MailMerge.GetFieldNames();
Expand All @@ -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");
Expand All @@ -448,6 +450,7 @@ public void MappedDataFields()
public void DeleteFields()
{
//ExStart:DeleteFields
//GistId:f39874821cb317d245a769c9ce346fea
Document doc = new Document();

doc.MailMerge.DeleteFields();
Expand Down

0 comments on commit fe2e4ef

Please sign in to comment.