Blank Space Issue

I have issues with blank space as well. I did try to set up as you had described earlier. And tried using \b switch too. Can you show again what I am doing wrong and also the requirement is to have . if middlename exist. Issue is if its null its still inserting blank space. I have tried \b as well if its null.

Regex regex = new Regex(" ");
FindReplaceOptions options = new FindReplaceOptions();
options.IgnoreFields = true;

// Replace 'e' in document while ignoring deleted text.
options.IgnoreDeleted = true;
// doc.Range.Replace(regex, "", options);
doc.UpdateFields();
doc.Range.Replace(new Regex(@" <MiddleInitial>"), string.Empty, options);
doc.Range.Replace(new Regex(@" <PartyMiddleName>"), string.Empty, options);
doc.Range.Replace(new Regex(@" <MiddleName>"), string.Empty, options);
doc.Range.Replace(new Regex(@" <Vasrd>,"), string.Empty, options);
doc.Range.Replace(new Regex(@" <crsceffdates>,"), string.Empty, options);
doc.Range.Replace(new Regex(@" <Vasrd1>,"), string.Empty, options);
doc.Range.Replace(new Regex(@" <Vasrd2>,"), string.Empty, options);
doc.Range.Replace(new Regex(@" <Vasrd3>,"), string.Empty, options);
doc.Range.Replace(new Regex(@" <LatestDisabilityPercentage>,"), string.Empty, options);
doc.Range.Replace(new Regex(@" <VADiagnosticDesc>,"), string.Empty, options);
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyTableRows | MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveContainingFields;
doc.MailMerge.DeleteFields();
CleanupOptions cleanupOptions = new CleanupOptions();
cleanupOptions.UnusedBuiltinStyles = true;
doc.Cleanup(cleanupOptions);

if (DT.Rows[0]["PartyMiddleName"].ToString() != "")
{
    DT.Rows[0]["MiddleInitial"] = ' ' + Util.FormatTextToUpper(DT.Rows[0]["PartyMiddleName"].ToString().Substring(0, 1) + ".");
}
else
{
    DT.Rows[0]["MiddleInitial"] = "\b";
}

A CH61 APPROVAL (KL) (5).docx (88.5 KB)

@vlbuch23 You should setup your template like I suggested in this answer, i.e. use Text After feature of merge field. Field code should look like this: { MERGEFIELD FirstName \f " " }{ MERGEFIELD MidName \f " " }{ MERGEFIELD LastName } As you can see \f " " switch is added to the first two fields, if the field is not empty the text of this switch is added after the field value.

Hello, Alexey,

I have tried many things but this is still an issue. Including switch examples I have tried: - «MiddleName»
I have attached two documents. One with output and one that is like template that generate fields.A CRSC RECON DENIAL (TDS).docx (70.3 KB)
A CRSC RECON DENIAL (TDS) (10).docx (67.2 KB)

In C# I have it as : -

if (DT.Rows[0]["PartyMiddleName"].ToString() != "")
{
    DT.Rows[0]["MiddleInitial"] = " " + Util.FormatTextToUpper(DT.Rows[0]["PartyMiddleName"].ToString().Substring(0, 1) + "." + " ");
}
else
{
    DT.Rows[0]["MiddleInitial"] = "\b";
}

@vlbuch23 As I can see you did not modified your template as I suggested. You have put \f field with an empty string, but there must be a whitespace between quotes. Please find the modified template: modified.docx (70.7 KB)
Press Alt+F9 in MS Word to see field codes.

So I inserted yours modified template and tried. Modified my C# code as well. Still inserting extra space if middlename is null. Why? And it seems like it is inserting blank space. If it has a milddlename it is working fine with dot as expected.

A CRSC RECON DENIAL (TDS) (19) (2).docx (129.0 KB)

if (DT.Rows[0]["PartyMiddleName"].ToString() != "")
{
    DT.Rows[0]["MiddleInitial"] = Util.FormatTextToUpper(DT.Rows[0]["PartyMiddleName"].ToString().Substring(0, 1) + ".");
}

@vlbuch23 I have tested with the modified template and the following code and no redundant whitespaces are produced in the output:

String[] fieldNames = new String[] {"FirstName","MiddleName", "LastName"};
String[] fieldValues = new String[] {"Alexey","", "Noskov"};

Document doc = new Document("C:\\Temp\\modified.docx");
doc.getMailMerge().execute(fieldNames, fieldValues);

doc.save("C:\\Temp\\out.docx");

Hello I think you misunderstood my questions.
By default it is now inserting blank space for middlename.

I need a IF example that alllows you to remove null value for the mergefield if the value is not present.

So if you can please provide me an example of that.

I have also tried the following: -
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs | MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.MergeDuplicateRegions = true;
doc.MailMerge.ExecuteWithRegions(ds);
string[] mergeFields = doc.MailMerge.GetFieldNames();

I have also tried the following in document template:
{ IF { MERGEFIELD MIDDLENAME } = “” “” “” }

@vlbuch23 Yes, unfortunately, your requirements are not clear enough. So, could you please provide a simplified template, sample data, current and expected output document with highlighted problems. We will investigate the problem and try to help you.
If you would like to print some value in the output document if value of merge field is an empty string you can use IF field like this:
{ IF "{ MERGEFIELD test }"="" "Value is empty string" "Value is not empty" }

Problem is it puts extra blank space if middlename is null. I do not want that.

So basically if MiddleName is null it should not put any space. But if it has a value then with the value should be shown.

I tried like this and template is attached. Let me know.
«FIRSTNAME» { IF “{ MERGEFIELD MIDDLENAME }”="" “Value is empty string” “Value is not empty” }«MIDDLENAME» «LASTNAME»«SUFFIX», «BRANCH» (RET),A CRSC RECONSIDERATION APPROVAL (TDS) (1).docx (73.5 KB)

@vlbuch23 Thank you for additional information. But the scenario you have described is exactly what \f switch I have suggested earlier does. Once again. Here is a simple template with 3 merge fields: simple.docx (19.6 KB)
If fill the template with the following code (node that MiddleName value is null):

String[] fieldNames = new String[] {"FirstName", "MiddleName", "LastName"};
String[] fieldValues = new String[] {"Alexey", null, "Noskov"};

Document doc = new Document("C:\\Temp\\simple.docx");
doc.getMailMerge().execute(fieldNames, fieldValues);

doc.save("C:\\Temp\\out.docx");

There will be only one whitespace between first name and last name in the output:

If value of MiddleName in your data source is not null, value with a whitespace will be inserted:

String[] fieldNames = new String[] {"FirstName", "MiddleName", "LastName"};
String[] fieldValues = new String[] {"Alexey", "V", "Noskov"};

Here is the output if execute mail merge with this data source:

When you use \f (text to be inserted after field value) switch, the value of \f switch is not inserted if the value is null or empty string.

So can you provide example with /f switch then please
because when I tried like below did not work. Still a null space is there. So if no value should not insert anything.
MERGEFIELD MiddleName \f " "

@vlbuch23 The example with \f switch is already provided in my previous answer. See the attached template and code.

Ok that did it actually. Tested back and forth. Now no null values appear. F switch did the truck.

1 Like

Hello,

Is there an example if Middlename contains a dot then dont include it. I wanted if statement like that.
Thanks
Surbhi

@vlbuch23 Unfortunately, there is no way to add such condition in the template. But you can trim dot while preparing data for mail merge or upon executing mail merge in IFiedlMergingCallback:

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.FieldMergingCallback = new TrimDotCallback();
doc.MailMerge.Execute(new string[] { "Name" }, new object[] { "Alexey." });
doc.Save(@"C:\Temp\out.docx");
private class TrimDotCallback : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldName == "Name")
        {
            string value = args.FieldValue.ToString();
            if (value.EndsWith("."))
                args.FieldValue = value.TrimEnd('.');
        }
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        throw new NotImplementedException();
    }
}

Hello Can we not do it using the switch from the aspose word template?

@vlbuch23 As I have mentioned, unfortunately, there is no way to add such condition in the template.