|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.Drawing; |
| 4 | +using System; |
| 5 | +using System.Data; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace Remove_empty_column_after_mail_merge |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + //Boolean to check whether the merge field has value |
| 13 | + public static bool hasCostValue = false; |
| 14 | + //Cell index of the merge field |
| 15 | + public static int cellIndex; |
| 16 | + static void Main(string[] args) |
| 17 | + { |
| 18 | + //Creates new Word document instance for Word processing |
| 19 | + using (WordDocument document = new WordDocument()) |
| 20 | + { |
| 21 | + //Opens the Word template document |
| 22 | + Stream docStream = File.OpenRead(Path.GetFullPath(@"Data/Template.docx")); |
| 23 | + document.Open(docStream, FormatType.Docx); |
| 24 | + docStream.Dispose(); |
| 25 | + //Get the table |
| 26 | + WTable table = GetColumnIndex(document); |
| 27 | + //Get the data set |
| 28 | + DataSet ds = GetData(); |
| 29 | + //Using Merge events to do conditional formatting during runtime |
| 30 | + document.MailMerge.MergeField += new MergeFieldEventHandler(MergeField_TaskCost); |
| 31 | + //Execute Mail Merge with groups |
| 32 | + document.MailMerge.ExecuteGroup(ds.Tables["Task_CostList"]); |
| 33 | + if (!hasCostValue) |
| 34 | + { |
| 35 | + //Remove the empty column |
| 36 | + RemoveColumn(table); |
| 37 | + } |
| 38 | + //Saves and closes the Word document |
| 39 | + docStream = File.Create(Path.GetFullPath(@"Output/Output.docx")); |
| 40 | + document.Save(docStream, FormatType.Docx); |
| 41 | + docStream.Dispose(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + #region Helper Methods |
| 46 | + /// <summary> |
| 47 | + /// Get the column index |
| 48 | + /// </summary> |
| 49 | + private static WTable GetColumnIndex(WordDocument document) |
| 50 | + { |
| 51 | + WTable table = null; |
| 52 | + //Get the merge field |
| 53 | + WMergeField mergeField = document.FindItemByProperty(EntityType.MergeField, "FieldName", "Cost") as WMergeField; |
| 54 | + if (mergeField != null) |
| 55 | + { |
| 56 | + //Check whether the merge field is present inside a table cell |
| 57 | + if (mergeField.OwnerParagraph.IsInCell) |
| 58 | + { |
| 59 | + WTableCell cell = mergeField.OwnerParagraph.OwnerTextBody as WTableCell; |
| 60 | + //Get the column index |
| 61 | + cellIndex = cell.GetCellIndex(); |
| 62 | + table = cell.OwnerRow.Owner as WTable; |
| 63 | + } |
| 64 | + } |
| 65 | + return table; |
| 66 | + } |
| 67 | + /// <summary> |
| 68 | + /// Gets the data to perform mail merge |
| 69 | + /// </summary> |
| 70 | + private static DataSet GetData() |
| 71 | + { |
| 72 | + // Create a DataSet. |
| 73 | + DataSet ds = new DataSet(); |
| 74 | + //List of Syncfusion products name |
| 75 | + string[] products = { "Task 1", "Task 2", "Task 3", "Task 4", "Task 5" }; |
| 76 | + //Add new Tables to the data set |
| 77 | + DataRow row; |
| 78 | + ds.Tables.Add(); |
| 79 | + ds.Tables.Add(); |
| 80 | + //Add fields to the Task_CostList table. |
| 81 | + ds.Tables[0].TableName = "Task_CostList"; |
| 82 | + ds.Tables[0].Columns.Add("Task"); |
| 83 | + ds.Tables[0].Columns.Add("Cost"); |
| 84 | + int count = 0; |
| 85 | + //Insert values to the table row. |
| 86 | + foreach (string product in products) |
| 87 | + { |
| 88 | + row = ds.Tables["Task_CostList"].NewRow(); |
| 89 | + row["Task"] = product; |
| 90 | + ds.Tables["Task_CostList"].Rows.Add(row); |
| 91 | + count++; |
| 92 | + } |
| 93 | + return ds; |
| 94 | + } |
| 95 | + /// <summary> |
| 96 | + /// Remove the column |
| 97 | + /// </summary> |
| 98 | + private static void RemoveColumn(WTable table) |
| 99 | + { |
| 100 | + //Iterate through all rows |
| 101 | + for (int i = table.Rows.Count - 1; i >= 0; i--) |
| 102 | + { |
| 103 | + //Remove the cell present in the cellIndex |
| 104 | + table.Rows[i].Cells.RemoveAt(cellIndex); |
| 105 | + } |
| 106 | + } |
| 107 | + #endregion |
| 108 | + |
| 109 | + #region Event Handlers |
| 110 | + /// <summary> |
| 111 | + /// Method to handle MergeImageField event. |
| 112 | + /// </summary> |
| 113 | + private static void MergeField_TaskCost(object sender, MergeFieldEventArgs args) |
| 114 | + { |
| 115 | + if ( args.FieldName == "Cost" && hasCostValue && args.FieldValue != null |
| 116 | + && args.FieldValue != DBNull.Value && args.FieldValue != string.Empty) |
| 117 | + { |
| 118 | + hasCostValue = true; |
| 119 | + } |
| 120 | + } |
| 121 | + #endregion |
| 122 | + } |
| 123 | +} |
0 commit comments