Skip to content

Instantly share code, notes, and snippets.

@aspose-words-gists
Last active January 20, 2025 13:04
Show Gist options
  • Save aspose-words-gists/b4bab1bf22437a86d8062e91cf154494 to your computer and use it in GitHub Desktop.
Save aspose-words-gists/b4bab1bf22437a86d8062e91cf154494 to your computer and use it in GitHub Desktop.
Aspose.Words for .NET. Advanced Mail Merge features using C#.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
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.
FieldSkipIf fieldSkipIf = (FieldSkipIf)builder.InsertField(FieldType.FieldSkipIf, true);
fieldNextIf.LeftExpression = "5";
fieldNextIf.RightExpression = "2 + 3";
fieldNextIf.ComparisonOperator = "=";
doc.Save(ArtifactsDir + "WorkingWithFields.FieldNext.docx");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
string[] fieldNames = doc.MailMerge.GetFieldNames();
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Mail merge regions.docx");
MailMergeRegionInfo regionInfo = doc.MailMerge.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);
regions = doc.MailMerge.GetRegionsByName("Region2");
Assert.AreEqual(1, doc.MailMerge.GetRegionsByName("Region2").Count);
foreach (MailMergeRegionInfo region in regions) Assert.AreEqual("Region2", region.Name);
regions = doc.MailMerge.GetRegionsByName("NestedRegion1");
Assert.AreEqual(2, doc.MailMerge.GetRegionsByName("NestedRegion1").Count);
foreach (MailMergeRegionInfo region in regions) Assert.AreEqual("NestedRegion1", region.Name);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
MailMergeRegionInfo regionInfo = doc.MailMerge.GetRegionsHierarchy();
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
doc.MailMerge.MappedDataFields.Add("MyFieldName_InDocument", "MyFieldName_InDataSource");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment