Hello,
Thanks for your inquiry!
Do you use the style of the paragraph to identify if the paragraph needs to be deleted?
If so, please refer to the following code to use MergeFieldEventHandler to achieve your needs of remove #Heading 2. Here I also attached my input file. But I do not understand how you judge the #Paragraph 4 and #Paragraph 5. To help us solve your issue quickly and efficiently, please provide us your input file for reference, thanks in advance.
- Code: Select all
Document document = new Document();
document.LoadFromFile(@"E:\testdoc\doc3.docx");
List<string> FieldName = new List<string>();
FieldName.Add("first");
FieldName.Add("second");
FieldName.Add("third");
FieldName.Add("fourth");
FieldName.Add("fifth");
List<string> values = new List<string>();
values.Add("aaa");
values.Add("bbb");
values.Add("ccc");
values.Add("ddd");
values.Add("eee");
document.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);
void MailMerge_MergeField(object sender, MergeFieldEventArgs args1)
{
Console.WriteLine(args1.CurrentMergeField.OwnerParagraph.StyleName);
if(args1.CurrentMergeField.OwnerParagraph.StyleName.Equals("Heading1"))
{
args1.CurrentMergeField.OwnerParagraph.OwnerTextBody.ChildObjects.Remove(args1.CurrentMergeField.OwnerParagraph);
}
}
document.MailMerge.Execute(FieldName.ToArray(), values.ToArray());
Console.ReadKey();
document.SaveToFile("after remove.docx");
If I have any misunderstand, please provide us with your input file, and you expect output file for reference.
Sincerely,
Marcia
E-iceblue support team
Login to view the files attached to this post.