Hi Team,
using the below code everything is working fine but I am facing one issue my colon( 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( 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);
}
}
}
}
}