Word Merge

The best way to create tags is to use existing standard standard tools such as Microsoft Word.

How do you do this and configure delivery labels?

I'm not sure how to map merge fields to columns of a data grid view. This is the code that I still have:

// Create a new empty document. DocumentModel document = new DocumentModel(); // Add document content. document.Sections.Add( new Section(document, new Paragraph(document, new Field(document, FieldType.MergeField, "FullName")))); // Save the document to a file and open it with Microsoft Word. document.Save("TemplateDocument.docx"); // If document appears empty in Microsoft Word, press Alt + F9. Process.Start("TemplateDocument.docx"); // Initialize mail merge data source. var dataSource = new { FullName = "John Doe" }; // Execute mail merge. document.MailMerge.Execute(dataSource); // Save the document to a file and open it with Microsoft Word. document.Save("Document.docx"); Process.Start("Document.docx"); 
+4
source share
1 answer

First, you must learn how to make a Label template document. For example, following this video tutorial: www.youtube.com/watch?v=tIg70utT72Q

Before saving the template document, delete the merge data source created during the merge because you will use the .NET object as the merge data source. To delete a merge data source, go to the Mailing tab โ†’ Start merging โ†’ select โ€œPlain Word documentโ€, for example, in the image: Remove mail merge data source

Save the document to a file, for example, "LabelTemplate.docx". When you press Alt + F9, you should see the field codes, as in the following image: Label template content

Now you are ready to merge the program letters with the GemBox.Document component (what code are you using in the question). Here is the C # code to do the merge:

 // Set licensing info. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); ComponentInfo.FreeLimitReached += (sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial; // Create mail merge data source. // You should use DataGridView.DataSource property - it will return DataView or DataTable that is data-bound to your DataGridView. var dataTable = new DataTable() { Columns = { new DataColumn("Name"), new DataColumn("Surname"), new DataColumn("Company") }, Rows = { { "John", "Doe", "ACME" }, { "Fred", "Nurk", "ACME" }, { "Hans", "Meier", "ACME" } } }; var document = DocumentModel.Load("LabelTemplate.docx", LoadOptions.DocxDefault); // Use this if field names and data column names differ. If they are the same (case-insensitive), then you don't need to define mappings explicitly. document.MailMerge.FieldMappings.Add("First_Name", "Name"); document.MailMerge.FieldMappings.Add("Last_Name", "Surname"); document.MailMerge.FieldMappings.Add("Company_Name", "Company"); // Execute mail merge. Each mail merge field will be replaced with the data from the data source appropriate row and column. document.MailMerge.Execute(dataTable); // Remove any left mail merge field. document.MailMerge.RemoveMergeFields(); // Save resulting document to a file. document.Save("Labels.docx"); 

The resulting Labels.docx document should look like this: Label mail merge result

An ad headline is added automatically because the component is used in trial mode. The GemBox.Document mail merge is very flexible, it supports hierarchical mail merge by setting up the merge of each field, handling the FieldMerging event, or specifying the format string of the merge field .

+1
source

All Articles