private void MergeDynamicTableFields(DocumentBuilder mBuilder, FieldMergingArgs e) { try { if (e.FieldName.Contains("tbl_PressSearch") && e.FieldValue != null) { this.ReplaceFieldWithDynamicDataSetForPressSearchOrAppendix(mBuilder, e); } else if (e.FieldName.Contains("tbl_Appendix") && e.FieldValue != null) { this.ReplaceFieldWithDynamicDataSetForPressSearchOrAppendix(mBuilder, e); } else if (e.FieldName.ToLower().Contains("tbl_") && e.FieldValue != null) //All merge fields for tables start with "tbl_" prefix { this.ReplaceFieldWithDynamicDataSet(mBuilder, e); } } catch (Exception ex) { if (HttpContext.Current.Session["Error"] != null && !string.IsNullOrEmpty((string)HttpContext.Current.Session["Error"])) { HttpContext.Current.Session["Error"] = "Error occured while making function call to merge table place holder field with dynamic table || " + (string)HttpContext.Current.Session["Error"]; } else { HttpContext.Current.Session["Error"] = "Error occured while making function call to merges table place holder field with dynamic table || " + ex.Message; } throw ex; } }
/// <summary> /// 插入签名图片 /// </summary> /// <param name="args"></param> /// <param name="userID"></param> private void InsertSign(FieldMergingArgs args, string userID) { IUserService service = FormulaHelper.GetService <IUserService>(); byte[] bytes = service.GetSignImg(userID); DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.FieldName); if (bytes != null) { Shape shape = builder.InsertImage(bytes); shape.Top = 10; shape.Height = 30; shape.Width = 80; } else { UserInfo user = service.GetUserInfoByID(userID); if (user != null) { builder.InsertHtml(user.UserName); } else { builder.InsertHtml(""); } } }
/// <summary> /// This handler makes special processing for the "Document_1" field. /// The field value contains the path to load the document. /// We load the document and insert it into the current merge field. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName == "Document_1") { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName); // Load the document from the blob field. MemoryStream stream = new MemoryStream((byte[])e.FieldValue); Document subDoc = new Document(stream); // Insert the document. InsertDocument(builder.CurrentParagraph, subDoc); // The paragraph that contained the merge field might be empty now and you probably want to delete it. if (!builder.CurrentParagraph.HasChildNodes) { builder.CurrentParagraph.Remove(); } // Indicate to the mail merge engine that we have inserted what we wanted. e.Text = null; } }
/// <summary> /// This handler is called for every mail merge field found in the document, /// for every record found in the data source. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (mBuilder == null) mBuilder = new DocumentBuilder(e.Document); // We decided that we want all boolean values to be output as check box form fields. if (e.FieldValue is bool) { // Move the "cursor" to the current merge field. mBuilder.MoveToMergeField(e.FieldName); // It is nice to give names to check boxes. Lets generate a name such as MyField21 or so. string checkBoxName = string.Format("{0}{1}", e.FieldName, e.RecordIndex); // Insert a check box. mBuilder.InsertCheckBox(checkBoxName, (bool)e.FieldValue, 0); // Nothing else to do for this field. return; } // Another example, we want the Subject field to come out as text input form field. if (e.FieldName == "Subject") { mBuilder.MoveToMergeField(e.FieldName); string textInputName = string.Format("{0}{1}", e.FieldName, e.RecordIndex); mBuilder.InsertTextInput(textInputName, TextFormFieldType.Regular, "", (string)e.FieldValue, 0); } }
// [ischoolkingdom] Vicky新增,[11-04][02]在學證明書(英文),新增照片 void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.FieldName == "新生照片") { byte[] photo = e.FieldValue as byte[]; if (photo == null) { return; } DocumentBuilder photoBuilder = new DocumentBuilder(e.Document); photoBuilder.MoveToField(e.Field, true); e.Field.Remove(); Shape photoShape = new Shape(e.Document, ShapeType.Image); photoShape.ImageData.SetImage(photo); double shapeHeight = 0; double shapeWidth = 0; photoShape.WrapType = WrapType.Inline;//設定文繞圖 //resize double origSizeRatio = photoShape.ImageData.ImageSize.HeightPoints / photoShape.ImageData.ImageSize.WidthPoints; Cell curCell = photoBuilder.CurrentParagraph.ParentNode as Cell; shapeHeight = (curCell.ParentNode as Row).RowFormat.Height; shapeWidth = curCell.CellFormat.Width; photoShape.Height = shapeHeight; photoShape.Width = shapeWidth; photoBuilder.InsertNode(photoShape); } }
public void FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName.EndsWith("Html")) { // Insert the text for this merge field as HTML data, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.DocumentFieldName); builder.InsertHtml((string)args.FieldValue); while (!builder.CurrentParagraph.HasChildNodes) { // Get previouse node. Node prevNode = builder.CurrentParagraph.PreviousSibling; // we do not need to remove current paragraph if th epreviouse node is table for example. if (prevNode == null || prevNode.NodeType != NodeType.Paragraph) { break; } builder.CurrentParagraph.Remove(); builder.MoveTo(prevNode); } // The HTML text itself should not be inserted. // We have already inserted it as an HTML. args.Text = ""; } return; }
/// <summary> /// Called for every merge field encountered in the document. /// We can either return some data to the mail merge engine or do something /// else with the document. In this case we modify cell formatting. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (mBuilder == null) { mBuilder = new DocumentBuilder(args.Document); } // This way we catch the beginning of a new row. if (args.FieldName.Equals("CompanyName")) { // Select the color depending on whether the row number is even or odd. Color rowColor; if (IsOdd(mRowIdx)) { rowColor = Color.FromArgb(213, 227, 235); } else { rowColor = Color.FromArgb(242, 242, 242); } // There is no way to set cell properties for the whole row at the moment, // so we have to iterate over all cells in the row. for (int colIdx = 0; colIdx < 4; colIdx++) { mBuilder.MoveToCell(0, mRowIdx, colIdx, 0); mBuilder.CellFormat.Shading.BackgroundPatternColor = rowColor; } mRowIdx++; } }
/// <summary> /// This handler is called for every mail merge field found in the document, /// for every record found in the data source. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (mBuilder == null) { mBuilder = new DocumentBuilder(e.Document); } // We decided that we want all boolean values to be output as check box form fields. if (e.FieldValue is bool) { // Move the "cursor" to the current merge field. mBuilder.MoveToMergeField(e.FieldName); // It is nice to give names to check boxes. Lets generate a name such as MyField21 or so. string checkBoxName = string.Format("{0}{1}", e.FieldName, e.RecordIndex); // Insert a check box. mBuilder.InsertCheckBox(checkBoxName, (bool)e.FieldValue, 0); // Nothing else to do for this field. return; } // Another example, we want the Subject field to come out as text input form field. if (e.FieldName == "Subject") { mBuilder.MoveToMergeField(e.FieldName); string textInputName = string.Format("{0}{1}", e.FieldName, e.RecordIndex); mBuilder.InsertTextInput(textInputName, TextFormFieldType.Regular, "", (string)e.FieldValue, 0); } }
/// <summary> /// Called for each field belonging to an unmerged region in the document. /// </summary> public void FieldMerging(FieldMergingArgs args) { //ExStart:RemoveExtraParagraphs // Store the parent paragraph of the current field for easy access. Paragraph parentParagraph = args.Field.Start.ParentParagraph; // Define the logic to be used when the ContactDetails region is encountered. // The region is removed and replaced with a single line of text stating that there are no records. if (args.TableName == "ContactDetails") { // Called for the first field encountered in a region. This can be used to execute logic on the first field // in the region without needing to hard code the field name. Often the base logic is applied to the first field and // different logic for other fields. The rest of the fields in the region will have a null FieldValue. if ((string)args.FieldValue == "FirstField") { // Remove the "Name:" tag from the start of the paragraph parentParagraph.Range.Replace("Name:", string.Empty, false, false); // Set the text of the first field to display a message stating that there are no records. args.Text = "No records to display"; } else { // We have already inserted our message in the paragraph belonging to the first field. The other paragraphs in the region // will still remain so we want to remove these. A check is added to ensure that the paragraph has not already been removed. // which may happen if more than one field is included in a paragraph. if (parentParagraph.ParentNode != null) { parentParagraph.Remove(); } } } //ExEnd:RemoveExtraParagraphs //ExStart:MergeAllCells // Replace the unused region in the table with a "no records" message and merge all cells into one. if (args.TableName == "Suppliers") { if ((string)args.FieldValue == "FirstField") { // We will use the first paragraph to display our message. Make it centered within the table. The other fields in other cells // within the table will be merged and won't be displayed so we don't need to do anything else with them. parentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center; args.Text = "No records to display"; } // Merge the cells of the table together. Cell cell = (Cell)parentParagraph.GetAncestor(NodeType.Cell); if (cell != null) { if (cell.IsFirstCell) { cell.CellFormat.HorizontalMerge = CellMerge.First; // If this cell is the first cell in the table then the merge is started using "CellMerge.First". } else { cell.CellFormat.HorizontalMerge = CellMerge.Previous; // Otherwise the merge is continued using "CellMerge.Previous". } } } //ExEnd:MergeAllCells }
/// <summary> /// Called for every merge field encountered in the document. /// We can either return some data to the mail merge engine or do something /// Else with the document. In this case we modify cell formatting. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (mBuilder == null) mBuilder = new DocumentBuilder(e.Document); // This way we catch the beginning of a new row. if (e.FieldName.Equals("CompanyName")) { // Select the color depending on whether the row number is even or odd. Color rowColor; if (IsOdd(mRowIdx)) rowColor = Color.FromArgb(213, 227, 235); else rowColor = Color.FromArgb(242, 242, 242); // There is no way to set cell properties for the whole row at the moment, // So we have to iterate over all cells in the row. for (int colIdx = 0; colIdx < 4; colIdx++) { mBuilder.MoveToCell(0, mRowIdx, colIdx, 0); mBuilder.CellFormat.Shading.BackgroundPatternColor = rowColor; } mRowIdx++; } }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { #region 處理照片 if (e.FieldName == "照片") { DocumentBuilder builder1 = new DocumentBuilder(e.Document); builder1.MoveToField(e.Field, true); byte[] photoBytes = null; try { photoBytes = Convert.FromBase64String("" + e.FieldValue); } catch (Exception ex) { //builder1.Write("照片粘貼處"); e.Field.Remove(); return; } if (photoBytes == null || photoBytes.Length == 0) { //builder1.Write("照片粘貼處"); e.Field.Remove(); return; } e.Field.Remove(); Shape photoShape = new Shape(e.Document, ShapeType.Image); photoShape.ImageData.SetImage(photoBytes); photoShape.WrapType = WrapType.Inline; #region AutoResize double origHWRate = photoShape.ImageData.ImageSize.HeightPoints / photoShape.ImageData.ImageSize.WidthPoints; double shapeHeight = (builder1.CurrentParagraph.ParentNode.ParentNode as Row).RowFormat.Height; double shapeWidth = (builder1.CurrentParagraph.ParentNode as Cell).CellFormat.Width; if ((shapeHeight / shapeWidth) < origHWRate) { shapeWidth = shapeHeight / origHWRate; } else { shapeHeight = shapeWidth * origHWRate; } #endregion photoShape.Height = shapeHeight * 0.9; photoShape.Width = shapeWidth * 0.9; builder1.InsertNode(photoShape); } #endregion }
//文本处理在这里 void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName.Equals("Desc")) { // 使用DocumentBuilder处理图片的大小 DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.FieldName); builder.InsertHtml(e.FieldValue.ToString()); } }
/// <summary> /// Called when a mail merge merges data into a MERGEFIELD. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.FieldName.StartsWith("text_")) { e.FieldValue = $"Merge value for \"{e.FieldName}\": {(string)e.FieldValue}"; } else if (e.FieldName.StartsWith("numeric_")) { e.FieldValue = (int)e.FieldValue * 1000; } }
public void FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName.Equals("name")) { // 使用DocumentBuilder处理图片的大小 DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.FieldName); builder.Font.Color = Color.BlueViolet; builder.Write(args.FieldValue.ToString()); } }
/// <summary> /// This is called for each merge field in the document /// when Document.MailMerge.ExecuteWithRegions is called. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName.Equals("CourseName")) { // Insert the checkbox for this merge field, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.FieldName); builder.InsertCheckBox(e.DocumentFieldName + this.mCheckBoxCount.ToString(), false, 0); builder.Write((string)e.FieldValue); this.mCheckBoxCount++; } }
/// <summary> /// This is called for each merge field in the document /// when Document.MailMerge.ExecuteWithRegions is called. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName.Equals("CourseName")) { // Insert the checkbox for this merge field, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.FieldName); builder.InsertCheckBox(args.DocumentFieldName + mCheckBoxCount, false, 0); builder.Write((string)args.FieldValue); mCheckBoxCount++; } }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.FieldName.Equals("mf2")) { DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.FieldName); builder.Font.Color = Color.Red; builder.StartBookmark("bm_" + e.RecordIndex); builder.Write(e.FieldValue.ToString()); builder.EndBookmark("bm_" + e.RecordIndex); } }
/// <summary> /// Called when a mail merge merges data into a MERGEFIELD. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName.StartsWith("html_") && args.Field.GetFieldCode().Contains("\\b")) { // Add parsed HTML data to the document's body. DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.DocumentFieldName); builder.InsertHtml((string)args.FieldValue); // Since we have already inserted the merged content manually, // we will not need to respond to this event by returning content via the "Text" property. args.Text = string.Empty; } }
/// <summary> /// This is called when merge field is actually merged with data in the document. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'. if (e.DocumentFieldName.StartsWith("html")) { // Insert the text for this merge field as HTML data, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName); builder.InsertHtml((string)e.FieldValue); // The HTML text itself should not be inserted. // We have already inserted it as an HTML. e.Text = ""; } }
/// <summary> /// This is called when merge field is actually merged with data in the document. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'. if (e.DocumentFieldName.StartsWith("html")) { // Insert the text for this merge field as HTML data, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName); builder.InsertHtml((string)e.FieldValue); // The HTML text itself should not be inserted. // We have already inserted it as an HTML. e.Text = ""; } }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (mBuilder == null) { mBuilder = new DocumentBuilder(e.Document); } // Our custom data source returns topic objects. Topic topic = (Topic)e.FieldValue; mBuilder.MoveToMergeField(e.FieldName); mBuilder.InsertHyperlink(topic.Title, topic.FileName, false); // Signal to the mail merge engine that it does not need to insert text into the field. e.Text = ""; }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.FieldName.StartsWith("HTML")) { if (e.Field.GetFieldCode().Contains("\\b")) { FieldMergeField field = e.Field; DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName, true, false); builder.Write(field.TextBefore); builder.InsertHtml(e.FieldValue.ToString()); e.Text = ""; } } }
/// <summary> /// This is called when merge field is actually merged with data in the document. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'. if (args.DocumentFieldName.StartsWith("html") && args.Field.GetFieldCode().Contains("\\b")) { FieldMergeField field = args.Field; // Insert the text for this merge field as HTML data, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.DocumentFieldName); builder.Write(field.TextBefore); builder.InsertHtml((string)args.FieldValue); // The HTML text itself should not be inserted. // We have already inserted it as an HTML. args.Text = ""; } }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.FieldValue == null) { return; } if (!e.FieldValue.ToString().StartsWith("<table") && !e.FieldValue.ToString().StartsWith("<html")) { e.Text = e.FieldValue.ToString(); return; } var builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName); builder.InsertHtml((string)e.FieldValue); e.Text = ""; }
/// <summary> /// Called when a mail merge merges data into a MERGEFIELD. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName == "CourseName") { Assert.AreEqual("StudentCourse", args.TableName); DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.FieldName); builder.InsertCheckBox(args.DocumentFieldName + mCheckBoxCount, false, 0); string fieldValue = args.FieldValue.ToString(); // In this case, for every record index 'n', the corresponding field value is "Course n". Assert.AreEqual(char.GetNumericValue(fieldValue[7]), args.RecordIndex); builder.Write(fieldValue); mCheckBoxCount++; } }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName == "Document_1") { DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.DocumentFieldName); Document subDoc = new Document((string)args.FieldValue); InsertDocument(builder.CurrentParagraph, subDoc); if (!builder.CurrentParagraph.HasChildNodes) { builder.CurrentParagraph.Remove(); } args.Text = null; } }
/// <summary> /// Called when a mail merge merges data into a MERGEFIELD. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (mBuilder == null) { mBuilder = new DocumentBuilder(args.Document); } // This is true of we are on the first column, which means we have moved to a new row. if (args.FieldName == "CompanyName") { Color rowColor = IsOdd(mRowIdx) ? Color.FromArgb(213, 227, 235) : Color.FromArgb(242, 242, 242); for (int colIdx = 0; colIdx < 4; colIdx++) { mBuilder.MoveToCell(0, mRowIdx, colIdx, 0); mBuilder.CellFormat.Shading.BackgroundPatternColor = rowColor; } mRowIdx++; } }
/// <summary> /// This handler makes special processing for the "Document_1" field. /// The field value contains the path to load the document. /// We load the document and insert it into the current merge field. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName == "Document_1") { DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName); MemoryStream stream = new MemoryStream((byte[])e.FieldValue); Document subDoc = new Document(stream); InsertDocument(builder.CurrentParagraph, subDoc); // The paragraph that contained the merge field might be empty now, and you probably want to delete it. if (!builder.CurrentParagraph.HasChildNodes) { builder.CurrentParagraph.Remove(); } e.Text = null; } }
/// <summary> /// This is called when merge field is actually merged with data in the document. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { switch (e.FieldName) { case "TextField": Assert.AreEqual("Original value", e.FieldValue); e.FieldValue = "New value"; break; case "TextField2": Assert.AreEqual("Original value", e.FieldValue); e.Text = "New value from FieldMergingArgs"; // Should suppress e.FieldValue and ignore format e.FieldValue = "new value"; break; case "NumericField": Assert.AreEqual(10.0d, e.FieldValue); e.FieldValue = 20; break; } }
/// <summary> /// Called for each field belonging to an unmerged region in the document. /// </summary> public void FieldMerging(FieldMergingArgs args) { // Change the text of each field of the ContactDetails region individually. if (args.TableName == "ContactDetails") { // Set the text of the field based off the field name. if (args.FieldName == "Name") { args.Text = "(No details found)"; } else if (args.FieldName == "Number") { args.Text = "(N/A)"; } } // Remove the entire table of the Suppliers region. Also check if the previous paragraph // before the table is a heading paragraph and if so remove that too. if (args.TableName == "Suppliers") { Table table = (Table)args.Field.Start.GetAncestor(NodeType.Table); // Check if the table has been removed from the document already. if (table.ParentNode != null) { // Try to find the paragraph which precedes the table before the table is removed from the document. if (table.PreviousSibling != null && table.PreviousSibling.NodeType == NodeType.Paragraph) { Paragraph previousPara = (Paragraph)table.PreviousSibling; if (IsHeadingParagraph(previousPara)) { previousPara.Remove(); } } table.Remove(); } } }
/// <summary> /// This is called for each merge field in the document /// when Document.MailMerge.ExecuteWithRegions is called. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName.Equals("CourseName")) { // The name of the table that we are merging can be found here Assert.AreEqual("StudentCourse", args.TableName); // Insert the checkbox for this merge field, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.FieldName); builder.InsertCheckBox(args.DocumentFieldName + mCheckBoxCount, false, 0); // Get the actual value of the field string fieldValue = args.FieldValue.ToString(); // In this case, for every record index 'n', the corresponding field value is "Course n" Assert.AreEqual(Char.GetNumericValue(fieldValue[7]), args.RecordIndex); builder.Write(fieldValue); mCheckBoxCount++; } }
// This handler makes special processing for the "Document_1" field. // The field value contains the path to load the document. // We load the document and insert it into the current merge field. void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (args.DocumentFieldName == "Document_1") { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.DocumentFieldName); // The name of the document to load and insert is stored in the field value. Document subDoc = new Document((string)args.FieldValue); InsertDocument(builder.CurrentParagraph, subDoc); // The paragraph that contained the merge field might be empty now, and you probably want to delete it. if (!builder.CurrentParagraph.HasChildNodes) { builder.CurrentParagraph.Remove(); } // Indicate to the mail merge engine that we have inserted what we wanted. args.Text = null; } }
/// <summary> /// Called for every merge field encountered in the document. /// We can either return some data to the mail merge engine or do something /// else with the document. In this case we modify cell formatting. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { if (mBuilder == null) { mBuilder = new DocumentBuilder(args.Document); } // This way we catch the beginning of a new row if (args.FieldName.Equals("CompanyName")) { // Select the color depending on whether the row number is even or odd Color rowColor = IsOdd(mRowIdx) ? Color.FromArgb(213, 227, 235) : Color.FromArgb(242, 242, 242); // Set properties for each cell in the row for (int colIdx = 0; colIdx < 4; colIdx++) { mBuilder.MoveToCell(0, mRowIdx, colIdx, 0); mBuilder.CellFormat.Shading.BackgroundPatternColor = rowColor; } mRowIdx++; } }
/// <summary> /// This handler is called for every mail merge field found in the document, /// for every record found in the data source. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (mBuilder == null) { mBuilder = new DocumentBuilder(e.Document); } // We decided that we want all boolean values to be output as check box form fields. if (e.FieldValue is bool) { // Move the "cursor" to the current merge field. mBuilder.MoveToMergeField(e.FieldName); string checkBoxName = $"{e.FieldName}{e.RecordIndex}"; mBuilder.InsertCheckBox(checkBoxName, (bool)e.FieldValue, 0); return; } switch (e.FieldName) { case "Body": mBuilder.MoveToMergeField(e.FieldName); mBuilder.InsertHtml((string)e.FieldValue); break; case "Subject": { mBuilder.MoveToMergeField(e.FieldName); string textInputName = $"{e.FieldName}{e.RecordIndex}"; mBuilder.InsertTextInput(textInputName, TextFormFieldType.Regular, "", (string)e.FieldValue, 0); break; } } }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (mBuilder == null) mBuilder = new DocumentBuilder(e.Document); // Our custom data source returns topic objects. Topic topic = (Topic)e.FieldValue; // We use the document builder to move to the current merge field and insert a hyperlink. mBuilder.MoveToMergeField(e.FieldName); mBuilder.InsertHyperlink(topic.Title, topic.FileName, false); // Signal to the mail merge engine that it does not need to insert text into the field // as we've done it already. e.Text = ""; }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args) { // Do nothing. }
/// <summary> /// Called for each field belonging to an unmerged region in the document. /// </summary> public void FieldMerging(FieldMergingArgs args) { //ExStart //ExId:ContactDetailsCodeVariation //ExSummary:Shows how to replace an unused region with a message and remove extra paragraphs. // Store the parent paragraph of the current field for easy access. Paragraph parentParagraph = args.Field.Start.ParentParagraph; // Define the logic to be used when the ContactDetails region is encountered. // The region is removed and replaced with a single line of text stating that there are no records. if (args.TableName == "ContactDetails") { // Called for the first field encountered in a region. This can be used to execute logic on the first field // in the region without needing to hard code the field name. Often the base logic is applied to the first field and // different logic for other fields. The rest of the fields in the region will have a null FieldValue. if ((string)args.FieldValue == "FirstField") { // Remove the "Name:" tag from the start of the paragraph parentParagraph.Range.Replace("Name:", string.Empty, false, false); // Set the text of the first field to display a message stating that there are no records. args.Text = "No records to display"; } else { // We have already inserted our message in the paragraph belonging to the first field. The other paragraphs in the region // will still remain so we want to remove these. A check is added to ensure that the paragraph has not already been removed. // which may happen if more than one field is included in a paragraph. if (parentParagraph.ParentNode != null) parentParagraph.Remove(); } } //ExEnd //ExStart //ExFor:Cell.IsFirstCell //ExId:SuppliersCodeVariation //ExSummary:Shows how to merge all the parent cells of an unused region and display a message within the table. // Replace the unused region in the table with a "no records" message and merge all cells into one. if (args.TableName == "Suppliers") { if ((string)args.FieldValue == "FirstField") { // We will use the first paragraph to display our message. Make it centered within the table. The other fields in other cells // within the table will be merged and won't be displayed so we don't need to do anything else with them. parentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center; args.Text = "No records to display"; } // Merge the cells of the table together. Cell cell = (Cell)parentParagraph.GetAncestor(NodeType.Cell); if (cell != null) { if (cell.IsFirstCell) cell.CellFormat.HorizontalMerge = CellMerge.First; // If this cell is the first cell in the table then the merge is started using "CellMerge.First". else cell.CellFormat.HorizontalMerge = CellMerge.Previous; // Otherwise the merge is continued using "CellMerge.Previous". } } //ExEnd }
/// <summary> /// Called for each field belonging to an unmerged region in the document. /// </summary> public void FieldMerging(FieldMergingArgs args) { // Change the text of each field of the ContactDetails region individually. if (args.TableName == "ContactDetails") { // Set the text of the field based off the field name. if (args.FieldName == "Name") args.Text = "(No details found)"; else if (args.FieldName == "Number") args.Text = "(N/A)"; } // Remove the entire table of the Suppliers region. Also check if the previous paragraph // before the table is a heading paragraph and if so remove that too. if (args.TableName == "Suppliers") { Table table = (Table)args.Field.Start.GetAncestor(NodeType.Table); // Check if the table has been removed from the document already. if (table.ParentNode != null) { // Try to find the paragraph which precedes the table before the table is removed from the document. if (table.PreviousSibling != null && table.PreviousSibling.NodeType == NodeType.Paragraph) { Paragraph previousPara = (Paragraph)table.PreviousSibling; if (IsHeadingParagraph(previousPara)) previousPara.Remove(); } table.Remove(); } } }
/// <summary> /// This handler makes special processing for the "Document_1" field. /// The field value contains the path to load the document. /// We load the document and insert it into the current merge field. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName == "Document_1") { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName); // Load the document from the blob field. MemoryStream stream = new MemoryStream((byte[])e.FieldValue); Document subDoc = new Document(stream); // Insert the document. InsertDocument(builder.CurrentParagraph, subDoc); // The paragraph that contained the merge field might be empty now and you probably want to delete it. if (!builder.CurrentParagraph.HasChildNodes) builder.CurrentParagraph.Remove(); // Indicate to the mail merge engine that we have inserted what we wanted. e.Text = null; } }
/// <summary> /// This handler makes special processing for the "Document_1" field. /// The field value contains the path to load the document. /// We load the document and insert it into the current merge field. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName == "Document_1") { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.DocumentFieldName); // The name of the document to load and insert is stored in the field value. Aspose.Words.Document subDoc = new Aspose.Words.Document((string)e.FieldValue); // Insert the document. InsertDocument(builder.CurrentParagraph, subDoc); // The paragraph that contained the merge field might be empty now and you probably want to delete it. if (!builder.CurrentParagraph.HasChildNodes) builder.CurrentParagraph.Remove(); // Indicate to the mail merge engine that we have inserted what we wanted. e.Text = null; } }
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { var builder = new DocumentBuilder(e.Document); if (e.Field.Start.GetAncestor(NodeType.HeaderFooter) != null) builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); builder.MoveToMergeField(e.FieldName); var value = string.Empty; if (e.FieldValue != null) value = (string)e.FieldValue; builder.InsertHtml(value); e.Text = ""; }
/// <summary> /// This is called for each merge field in the document /// when Document.MailMerge.ExecuteWithRegions is called. /// </summary> void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName.Equals("CourseName")) { // Insert the checkbox for this merge field, using DocumentBuilder. DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.FieldName); builder.InsertCheckBox(e.DocumentFieldName + this.mCheckBoxCount.ToString(), false, 0); builder.Write((string)e.FieldValue); this.mCheckBoxCount++; } }
void IFieldMergingCallback.FieldMerging( FieldMergingArgs args ) { }