I am removing extra space before and after colon :

Hi Team,

using the below code everything is working fine but I am facing one issue my colon(:slight_smile: Before the content is bold and the colon is also bold after the colon I have two spaces and the content is there that content is in normal text. below code is removing the extra space but after removing the extra space it’s replacing that space with 1st character in the text that time the 1st character is converted to bold. I don’t want to make that 1st charter bold please help me.

Please find below input and expected output :Expected_output101.docx (12.7 KB)
input101.docx (12.7 KB)

I tried to remove the before colon space and the after colon should be a single space if I have more than one space I need to remove extra space.
If after colon(:slight_smile: there is no space must and should we need to add.

Please find the below code I have used

public void AddDynamicSpacing(Document doc)
{


    FindReplaceOptions options = new FindReplaceOptions();
    options.UseSubstitutions = true;
    options.IgnoreDeleted = true;
    options.IgnoreFields = true;

    string before_value = "0";
    string after_value = "1";

    List<string> before_valueStringSpaces = new List<string>();
    for (var i = 0; i < Convert.ToInt32(before_value); i++)
    {
        before_valueStringSpaces.Add(" ");
    }
    var before_valuespaces = string.Join("", before_valueStringSpaces);

    List<string> after_valueStringSpaces = new List<string>();
    for (var i = 0; i < Convert.ToInt32(after_value); i++)
    {
        after_valueStringSpaces.Add(" ");
    }
    var after_valueSpaces = string.Join("", after_valueStringSpaces);
    var pattern = ":";

    if (label == "!" || label == "?")
    {
        if (after_value == "0")
        {
            pattern = string.Format(@"\{0}\s+([a-zA-Z])", label);
            doc.Range.Replace(new Regex(pattern), label + after_valueSpaces + "$1", options);

        }
        else
        {
            var range1 = Convert.ToInt32(after_value) - 1;
            var range2 = Convert.ToInt32(after_value) + 1;

            pattern = string.Format(@"((\{0}\saaa)|(\{0}\sbbb))([a-zA-Z])", label);
            pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
            pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
            doc.Range.Replace(new Regex(pattern), label + after_valueSpaces + "$4", options);
        }


        if (before_value == "0")
        {
            pattern = string.Format(@"([a-zA-Z])\s+\{0}", label);
            doc.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + label, options);
        }
        else
        {

            var range1 = Convert.ToInt32(before_value) - 1;
            var range2 = Convert.ToInt32(before_value) + 1;

            pattern = string.Format(@"([a-zA-Z])((\saaa)\{0}|(\sbbb)\{0})", label);
            pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
            pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
            doc.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + label, options);
        }
    }
    else
    {
        if (after_value == "0")
        {
            pattern = string.Format(@"{0}\s+([a-zA-Z])", label);
            doc.Range.Replace(new Regex(pattern), label + after_valueSpaces + "$1", options);

        }
        else
        {
            var range1 = Convert.ToInt32(after_value) - 1;
            var range2 = Convert.ToInt32(after_value) + 1;

            pattern = string.Format(@"(({0}\saaa)|({0}\sbbb))([a-zA-Z])", label);
            pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
            pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
            doc.Range.Replace(new Regex(pattern), label + after_valueSpaces + "$4", options);
        }


        if (before_value == "0")
        {
            pattern = string.Format(@"([a-zA-Z])\s+{0}", label);
            doc.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + label, options);
        }
        else
        {

            var range1 = Convert.ToInt32(before_value) - 1;
            var range2 = Convert.ToInt32(before_value) + 1;

            pattern = string.Format(@"([a-zA-Z])((\saaa){0}|(\sbbb){0})", label);
            pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
            pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
            doc.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + label, options);
        }
    }
}

OR

public class ValueList
{
    public string label { get; set; }
    public string name { get; set; }
    public string actual_label { get; set; }
    public string replace_label { get; set; }
    public string after_value_list { get; set; }
    public string before_value_list { get; set; }
    public string status { get; set; }
}
public void AddDynamicSpacing(RuleModel ruleModel, RuleBaseModel ruleBaseModel)
{
    //RuleCategory-PUNCTUATION  RuleSubcategory- Spacing After Punctuation    Rule - Add Dynamic Spacing   RuleComponent-PUNCTUATION

    ValueJSON valueJSON = Newtonsoft.Json.JsonConvert.DeserializeObject<ValueJSON>(ruleModel.ValueJSON);

    FindReplaceOptions options = new FindReplaceOptions();
    options.UseSubstitutions = true;
    options.IgnoreDeleted = true;
    options.IgnoreFields = true;
    foreach (var value in valueJSON.value_list)
    {
        if (value.status == "true")
        {

            string before_value = value.before_value_list;
            string after_value = value.after_value_list;

            List<string> before_valueStringSpaces = new List<string>();
            for (var i = 0; i < Convert.ToInt32(before_value); i++)
            {
                before_valueStringSpaces.Add(" ");
            }
            var before_valuespaces = string.Join("", before_valueStringSpaces);

            List<string> after_valueStringSpaces = new List<string>();
            for (var i = 0; i < Convert.ToInt32(after_value); i++)
            {
                after_valueStringSpaces.Add(" ");
            }
            var after_valueSpaces = string.Join("", after_valueStringSpaces);

            var pattern = "";

            if (value.label == "!" || value.label == "?")
            {
                if (after_value == "0")
                {
                    pattern = string.Format(@"\{0}\s+([a-zA-Z])", value.label);
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), value.label + after_valueSpaces + "$1", options);

                }
                else
                {
                    var range1 = Convert.ToInt32(after_value) - 1;
                    var range2 = Convert.ToInt32(after_value) + 1;

                    pattern = string.Format(@"((\{0}\saaa)|(\{0}\sbbb))([a-zA-Z])", value.label);
                    pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
                    pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), value.label + after_valueSpaces + "$4", options);
                }


                if (before_value == "0")
                {
                    pattern = string.Format(@"([a-zA-Z])\s+\{0}", value.label);
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + value.label, options);
                }
                else
                {

                    var range1 = Convert.ToInt32(before_value) - 1;
                    var range2 = Convert.ToInt32(before_value) + 1;

                    pattern = string.Format(@"([a-zA-Z])((\saaa)\{0}|(\sbbb)\{0})", value.label);
                    pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
                    pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + value.label, options);
                }
            }
            else
            {
                if (after_value == "0")
                {
                    pattern = string.Format(@"{0}\s+([a-zA-Z])", value.label);
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), value.label + after_valueSpaces + "$1", options);

                }
                else
                {
                    var range1 = Convert.ToInt32(after_value) - 1;
                    var range2 = Convert.ToInt32(after_value) + 1;

                    pattern = string.Format(@"(({0}\saaa)|({0}\sbbb))([a-zA-Z])", value.label);
                    pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
                    pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), value.label + after_valueSpaces + "$4", options);
                }


                if (before_value == "0")
                {
                    pattern = string.Format(@"([a-zA-Z])\s+{0}", value.label);
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + value.label, options);
                }
                else
                {

                    var range1 = Convert.ToInt32(before_value) - 1;
                    var range2 = Convert.ToInt32(before_value) + 1;

                    pattern = string.Format(@"([a-zA-Z])((\saaa){0}|(\sbbb){0})", value.label);
                    pattern = pattern.Replace("aaa", "{0," + range1.ToString() + "}");
                    pattern = pattern.Replace("bbb", "{" + range2.ToString() + ",}");
                    ruleBaseModel.SourceDocument.Range.Replace(new Regex(pattern), "$1" + before_valuespaces + value.label, options);
                }
            }

        }
    }
}

@Princeshivananjappa please check this code sample, it should fit your requirements:

Document doc = new Document("C:\\Temp\\input.docx");

Regex regBefore = new Regex(@"\s+(:)");
doc.Range.Replace(regBefore, ":");

Regex regAfter = new Regex(@"(:)\s\s+");
doc.Range.Replace(regAfter, ": ");


FindReplaceOptions opt = new FindReplaceOptions()
{
    ReplacingCallback = new ReplaceNoSpaceColonHandler()
};
Regex regNoSpace = new Regex(@"(:)[A-Za-z0-9]");
doc.Range.Replace(regNoSpace, ":", opt);


doc.Save("C:\\Temp\\output.docx");
 public class ReplaceNoSpaceColonHandler : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        var indx = args.MatchOffset + 1;
        var str1 = ((Run)args.MatchNode).Text.Substring(0, indx);
        var str2 = ((Run)args.MatchNode).Text.Length > indx ? ((Run)args.MatchNode).Text.Substring(indx) : string.Empty;
        ((Run)args.MatchNode).Text = str1 + " " + str2;

        return ReplaceAction.Skip;
    }
}

input.docx (12.8 KB)
output.docx (9.9 KB)

@eduardo.canal above code is working fine but I need to pass a dynamic value for colon before space and colon after space.
Note: make it dynamic for both before and after colon kindly help me asap

Any update on this?

@Princeshivananjappa thanks for the additional information, I’m looking into this to provide an updated version of the code.

Any Update on this?

@Princeshivananjappa here is an updated version of the code:

Document doc = new Document("C:\\Temp\\input.docx");

// Remove spaces before
Regex regBefore = new Regex(@"\s+(:)");
doc.Range.Replace(regBefore, ":");

// Remove spaces after
Regex regAfter = new Regex(@"(:)\s+");
doc.Range.Replace(regAfter, ":");


FindReplaceOptions opt = new FindReplaceOptions()
{
    ReplacingCallback = new AddWhiteSpacesHandler(0, 10)
};

doc.Range.Replace(":", ":", opt);

doc.Save("C:\\Temp\\output.docx");
public class AddWhiteSpacesHandler : IReplacingCallback
{
    private readonly int _spaceBefore;
    private readonly int _spaceAfter;

    public AddWhiteSpacesHandler(int spaceBefore, int spaceAfter)
    {
        _spaceBefore = spaceBefore;
        _spaceAfter = spaceAfter;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
    {
        var spacesBefore = "";
        for (var i = 0; i < _spaceBefore; i++)
        {
            spacesBefore += " ";
        }

        var spacesAfter = "";
        for (var i = 0; i < _spaceAfter; i++)
        {
            spacesAfter += " ";
        }

        var str1 = ((Run)args.MatchNode).Text.Substring(0, args.MatchOffset);
        var str2 = ((Run)args.MatchNode).Text.Substring(args.MatchOffset + 1);
        ((Run)args.MatchNode).Text = str1 + spacesBefore + ":" + spacesAfter + str2;

        return ReplaceAction.Skip;
    }
}