Skip to content

Instantly share code, notes, and snippets.

@aspose-words-gists
Last active July 11, 2024 04:23
Aspose.Words for .NET. Mail Merge template using C#.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
DocumentBuilder builder = new DocumentBuilder();
// Insert a text input field the unique name of this field is "Hello", the other parameters define
// what type of FormField it is, the format of the text, the field result and the maximum text length (0 = no limit)
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Hello", 0);
builder.InsertField(@"MERGEFIELD CustomerFirstName \* MERGEFORMAT");
builder.InsertTextInput("TextInput1", TextFormFieldType.Regular, "", " ", 0);
builder.InsertField(@"MERGEFIELD CustomerLastName \* MERGEFORMAT");
builder.InsertTextInput("TextInput1", TextFormFieldType.Regular, "", " , ", 0);
// Inserts a paragraph break into the document
builder.InsertParagraph();
// Insert mail body
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Thanks for purchasing our ", 0);
builder.InsertField(@"MERGEFIELD ProductName \* MERGEFORMAT");
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", ", please download your Invoice at ",
0);
builder.InsertField(@"MERGEFIELD InvoiceURL \* MERGEFORMAT");
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "",
". If you have any questions please call ", 0);
builder.InsertField(@"MERGEFIELD Supportphone \* MERGEFORMAT");
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", ", or email us at ", 0);
builder.InsertField(@"MERGEFIELD SupportEmail \* MERGEFORMAT");
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", ".", 0);
builder.InsertParagraph();
// Insert mail ending
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Best regards,", 0);
builder.InsertBreak(BreakType.LineBreak);
builder.InsertField(@"MERGEFIELD EmployeeFullname \* MERGEFORMAT");
builder.InsertTextInput("TextInput1", TextFormFieldType.Regular, "", " ", 0);
builder.InsertField(@"MERGEFIELD EmployeeDepartment \* MERGEFORMAT");
return builder.Document;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
args.ImageFileName = "Image.png";
args.ImageWidth.Value = 200;
args.ImageHeight = new MergeFieldImageDimension(200, MergeFieldImageDimensionUnit.Percent);
}
// 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 destinations - Fax.docx");
// Setup mail merge event handler to do the custom work.
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
// Trim trailing and leading whitespaces mail merge values.
doc.MailMerge.TrimWhitespaces = false;
string[] fieldNames = {
"RecipientName", "SenderName", "FaxNumber", "PhoneNumber",
"Subject", "Body", "Urgent", "ForReview", "PleaseComment"
};
object[] fieldValues = {
"Josh", "Jenny", "123456789", "", "Hello",
"<b>HTML Body Test message 1</b>", true, false, true
};
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(ArtifactsDir + "WorkingWithFields.MailMergeFormFields.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment