public void CleanupParagraphsWithPunctuationMarks() { //ExStart:CleanupParagraphsWithPunctuationMarks Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); FieldMergeField mergeFieldOption1 = (FieldMergeField)builder.InsertField("MERGEFIELD", "Option_1"); mergeFieldOption1.FieldName = "Option_1"; // Here is the complete list of cleanable punctuation marks: ! , . : ; ? ¡ ¿. builder.Write(" ? "); FieldMergeField mergeFieldOption2 = (FieldMergeField)builder.InsertField("MERGEFIELD", "Option_2"); mergeFieldOption2.FieldName = "Option_2"; doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs; // The option's default value is true, which means that the behavior was changed to mimic MS Word. // If you rely on the old behavior can revert it by setting the option to false. doc.MailMerge.CleanupParagraphsWithPunctuationMarks = true; doc.MailMerge.Execute(new[] { "Option_1", "Option_2" }, new object[] { null, null }); doc.Save(ArtifactsDir + "WorkingWithCleanupOptions.CleanupParagraphsWithPunctuationMarks.docx"); //ExEnd:CleanupParagraphsWithPunctuationMarks }
public void TestMailMergeGetRegionsHierarchy() { //ExStart //ExFor:MailMerge.GetRegionsHierarchy //ExFor:MailMergeRegionInfo //ExFor:MailMergeRegionInfo.Regions //ExFor:MailMergeRegionInfo.Name //ExFor:MailMergeRegionInfo.Fields //ExFor:MailMergeRegionInfo.StartField //ExFor:MailMergeRegionInfo.EndField //ExFor:MailMergeRegionInfo.Level //ExSummary:Shows how to get MailMergeRegionInfo and work with it Document doc = new Document(MyDir + "MailMerge.TestRegionsHierarchy.doc"); //Returns a full hierarchy of regions (with fields) available in the document. MailMergeRegionInfo regionInfo = doc.MailMerge.GetRegionsHierarchy(); //Get top regions in the document IList <MailMergeRegionInfo> topRegions = regionInfo.Regions; Assert.AreEqual(2, topRegions.Count); Assert.AreEqual("Region1", topRegions[0].Name); Assert.AreEqual("Region2", topRegions[1].Name); Assert.AreEqual(1, topRegions[0].Level); Assert.AreEqual(1, topRegions[1].Level); //Get nested region in first top region IList <MailMergeRegionInfo> nestedRegions = topRegions[0].Regions; Assert.AreEqual(2, nestedRegions.Count); Assert.AreEqual("NestedRegion1", nestedRegions[0].Name); Assert.AreEqual("NestedRegion2", nestedRegions[1].Name); Assert.AreEqual(2, nestedRegions[0].Level); Assert.AreEqual(2, nestedRegions[1].Level); //Get field list in first top region IList <Field> fieldList = topRegions[0].Fields; Assert.AreEqual(4, fieldList.Count); FieldMergeField startFieldMergeField = nestedRegions[0].StartField; Assert.AreEqual("TableStart:NestedRegion1", startFieldMergeField.FieldName); FieldMergeField endFieldMergeField = nestedRegions[0].EndField; Assert.AreEqual("TableEnd:NestedRegion1", endFieldMergeField.FieldName); //ExEnd }
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 = ""; } }
public static void Run() { //ExStart:InsertMergeFieldUsingDOM // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithFields(); Document doc = new Document(dataDir + "in.doc"); DocumentBuilder builder = new DocumentBuilder(doc); // Get paragraph you want to append this merge field to Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1]; // Move cursor to this paragraph builder.MoveTo(para); // We want to insert a merge field like this: // { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m \\v" } // Create instance of FieldMergeField class and lets build the above field code FieldMergeField field = (FieldMergeField)builder.InsertField(FieldType.FieldMergeField, false); // { " MERGEFIELD Test1" } field.FieldName = "Test1"; // { " MERGEFIELD Test1 \\b Test2" } field.TextBefore = "Test2"; // { " MERGEFIELD Test1 \\b Test2 \\f Test3 } field.TextAfter = "Test3"; // { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m" } field.IsMapped = true; // { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m \\v" } field.IsVerticalFormatting = true; // Finally update this merge field field.Update(); dataDir = dataDir + "InsertMergeFieldUsingDOM_out_.doc"; doc.Save(dataDir); //ExEnd:InsertMergeFieldUsingDOM Console.WriteLine("\nMerge field using DOM inserted successfully.\nFile saved at " + dataDir); }
public void RemoveColonBetweenEmptyMergeFields(string punctuationMark, bool isCleanupParagraphsWithPunctuationMarks, string resultText) { //ExStart //ExFor:MailMerge.CleanupParagraphsWithPunctuationMarks //ExSummary:Shows how to remove paragraphs with punctuation marks after mail merge operation. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); FieldMergeField mergeFieldOption1 = (FieldMergeField)builder.InsertField("MERGEFIELD", "Option_1"); mergeFieldOption1.FieldName = "Option_1"; // Here is the complete list of cleanable punctuation marks: // ! // , // . // : // ; // ? // ¡ // ¿ builder.Write(punctuationMark); FieldMergeField mergeFieldOption2 = (FieldMergeField)builder.InsertField("MERGEFIELD", "Option_2"); mergeFieldOption2.FieldName = "Option_2"; doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs; // The default value of the option is true which means that the behaviour was changed to mimic MS Word // If you rely on the old behavior are able to revert it by setting the option to false doc.MailMerge.CleanupParagraphsWithPunctuationMarks = isCleanupParagraphsWithPunctuationMarks; doc.MailMerge.Execute(new[] { "Option_1", "Option_2" }, new object[] { null, null }); doc.Save(ArtifactsDir + "RemoveColonBetweenEmptyMergeFields.docx"); //ExEnd Assert.AreEqual(resultText, doc.GetText()); }