We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to remove text before mail merge field using .NET

I want to know how to remove text from before mergeFields, e.g., In the word DOC
Name: <<Full_Name>>
Address: <<Person_Address>>
How can we remove Name: from the word if mergeFields(Full_Name) is empty!!
We can do it by changing the mergeFields and add text before: as “Name:”, but I don’t want to change the template!!
I need to remove text like Name: <<Full_Name>>, the entire para if Full_Name is empty!!

In java!!

@shaswatkr

Could you please ZIP and attach your input Word document here for our reference? We will then provide you code example to according to your requirement.

Example.zip (9.6 KB)
image.png (10.0 KB)

Here is what we need to do!!!
Thanks!!

@shaswatkr

In your case, we suggest you following solution.

  1. Please implement IFieldMergingCallback Interface.
  2. In IFieldMergingCallback.FieldMerging method, please check the value of mail merge field. If it is empty, get the paragraph node of mail merge field and remove it. Please check the following code snippet.

Please check the code example in the following article.
How to Execute Mail Merge


private class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldValue.ToString() == "")
        {
            args.Field.Start.ParentParagraph.Remove();
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing
    }
}

@tahir.manzoor

Thank you for this!! I’ll try it in my system!!!

Can you also help me with resizing of image??? I’m trying to insert an image into the mergeFields, but I need the image to be of specific size(which we’ll input in the code) and not the original size of image we’re using!!!

@tahir.manzoor,

Yeah thank you for the code!! It’s working!!!

But one problem is: It’s removing the para if mergeFields is in between.

Template: image.png (6.1 KB)
Output: image.png (20.1 KB)

As you can see njidnknjkdncjkcndjknckndjksd «Address» iojnkkkkkkkkkkkkkkk entire para is removed if we’re passing Address as “”!!

@shaswatkr

In IFieldMergingCallback.ImageFieldMerging method, you can use ImageFieldMergingArgs.ImageWidth property to set the image’s width and ImageFieldMergingArgs.ImageHeight property to set the image’s height.

In this case, we suggest you following solution.

  1. Get the paragraph node of field as suggested in my previous post.
  2. Get the first node of paragraph and check its node type.
  3. If it is not FieldStart, please remove it.
  4. Do the same step until paragraph’s first node type is FieldStart.
  5. Remove the field using Field.Remove method.

Please let us know if you have any more queries.

@tahir.manzoor

For the image resizing, I tried your suggestion and it’s working as we want!! Thanks!!

For the mergeFields in para!!! Can you send me the code??? I’m not able to understand the functions to use!!! Sorry for more work!!!

@shaswatkr

Following code example shows how to remove the text before mail merge field. Hope this helps you.

private class HandleMergeField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldValue.ToString() == "")
        {
            Node node = args.Field.Start.ParentParagraph.FirstChild;
            while (node.NodeType != NodeType.FieldStart)
                node.Remove();

            args.Field.Remove();
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing
    }
}