Issue in removing spaces between DOCVARIABLE

We have few predefined variables in doc template which will be replaced by its value dynamically while exporting.
If user adds more spaces in between variables, same reflects in exported word doc. We have handled a scenario where if one of the variable’s value is blank, it will not appear in the exported doc. But space entered by user preceding and suffixing that variable still stays.

e.g. "[var1] [var2] [var3] " with one space between all variables.
suppose the requirement is to replace above variables with below values,
[var1] = “A” [var2] = “” [var3] = “C”

after export output is coming as below,
“A C” with two spaces between A and C. Requirement is to truncate one space and show output as “A C”.

We are using below code to remove space,

objDocument.Range.Replace(" “,”",false,false)

But this removes space between all words in the scope of Range and not able to remove single space at a specific index.

Please suggest some way how can we remove one or more spaces at a specific index.

@gtodkar

Thanks for your inquiry. Please ZIP and attach your input Word document here for testing. We will investigate the issue on our side and provide you more information on this along with code.

@tahir.manzoor
Find attached file here,
Input.zip (9.3 KB)

@gtodkar

Thanks for sharing the document. Please use the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "Input.docx");
doc.UpdateFields();

foreach (Field field in doc.Range.Fields)
{
    if (field.Result.Trim().Equals(""))
        field.Remove();
}

doc.JoinRunsWithSameFormatting();
doc.Range.Replace(ControlChar.NonBreakingSpace, " ", new FindReplaceOptions());

int replace = 0;
do
{
    replace = doc.Range.Replace("  ", " ", new FindReplaceOptions());
    Console.WriteLine(replace);
} while (replace > 0);

doc.Save(MyDir + "17.7.docx");

@tahir.manzoor
Thank you so much for your reply. We will try this and update you soon.

@tahir.manzoor

We are using aspose 14.10 version where we couldn’t find the “ControlChar.NonBreakingSpace” and “FindReplaceOptions” definition in replace function.

Please suggest if there is any alternative option.

Thanks.

@gtodkar,

Thanks for your inquiry. Please try following code example. Hope this helps you.

Document doc = new Document(MyDir + "Input.docx");
doc.UpdateFields();

foreach (Field field in doc.Range.Fields)
{
    if (field.Result.Trim().Equals(""))
        field.Remove();
}

doc.JoinRunsWithSameFormatting();

int replace = 0;
do
{
    replace = doc.Range.Replace("  ", " ", false, false);
    Console.WriteLine(replace);
} while (replace > 0);

doc.Save(MyDir + "output.docx");

You are using very old version of Aspose.Words. Unfortunately, we don’t provide support for older released versions of Aspose.Words. Also, please note that we do not provide any fixes or patches for old versions of Aspose products either. All fixes and new features are always added into new versions of our products. So, please use latest version of Aspose.Words for .NET 17.7.

Hi Tahir,

Thanks for the quick resolution, The provided solution works but it removed the variable which we do not want to do.

Is there any way from we can remove spaces between document variables without removing the fields from the documents.

@gtodkar,

Thanks for your inquiry. You can workaround this issue by using following code example. Hope this helps you.

Document doc = new Document(MyDir + "Input.docx");
doc.UpdateFields();

foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldDocVariable)
    {
        if (field.Result.Length > 0 && field.Start.PreviousSibling != null && field.Start.PreviousSibling.NodeType == NodeType.Run
            && field.Start.PreviousSibling.GetText().Trim() == "")
            field.Start.PreviousSibling.Remove();
    }
}

doc.JoinRunsWithSameFormatting();

int replace = 0;
do
{
    replace = doc.Range.Replace("  ", " ", false, false);
    Console.WriteLine(replace);
} while (replace > 0);

doc.Save(MyDir + "output.docx");