@Karthik_Test_account the way to split a Paragraph in 2 (or more) is adding a ParagraphBreak in the desired Run, in this case to split correctly the text you need to reorder the Runs of the Paragraph in order to make the split after the character “}”. You can use the following code to achieve what you want:
Document doc = new Document("C:\\Temp\\input.docx");
Document clonDoc = (Document)doc.Clone(true);
DocumentBuilder builder = new DocumentBuilder(clonDoc);
int runIndex = -1;
int skipNewRuns = 0;
var runsForPageBreak = new List<int>();
// Reorder the Runs of the Paragraph to be able to set ParagraphBreak in the right place of the text
foreach (Run run in clonDoc.FirstSection.Body.FirstParagraph.Runs)
{
// Skip the Runs added in the previous iteration of the loop
if(skipNewRuns > 0)
{
skipNewRuns--;
continue;
}
runIndex++;
string text = run.Text;
// Get the breaking points in the text and place it in a new Run
var cutInedexes = text.Select((c, indx) => c == '}' ? indx + 1 : -1).Where(i => i >= 0).ToList();
if (!cutInedexes.Any()) continue;
for(int i = 0; i < cutInedexes.Count || i == 0; i++)
{
// If the breaking point is at the end of the text don't have text to put in a new Run
if (cutInedexes[i] == text.Length)
{
// If the break is at the end of the text, but this is not the last Run I need to insert a ParagraphBreak in this point
if (runIndex < clonDoc.FirstSection.Body.FirstParagraph.Runs.Count - 1)
{
// Save the index of the Run to insert a ParagraphBreak
runsForPageBreak.Add(runIndex + 1);
}
continue;
}
clonDoc.FirstSection.Body.FirstParagraph.Runs.Insert(runIndex + 1, new Run(clonDoc)
{
Text = cutInedexes.Count > 1 && i < cutInedexes.Count - 1 && cutInedexes[i + 1] < text.Length
? text.Substring(cutInedexes[i], cutInedexes[i+1])
: text.Substring(cutInedexes[i])
});
// Save the index of the Run to insert a ParagraphBreak
runsForPageBreak.Add(runIndex + 1);
skipNewRuns++;
runIndex++;
}
run.Text = text.Substring(0, cutInedexes[0]);
}
// Insert a ParagraphBreak in each recorded Run
for (var i = runsForPageBreak.Count - 1; i >= 0; i--)
{
builder.MoveTo(clonDoc.FirstSection.Body.FirstParagraph.Runs[runsForPageBreak[i]]);
builder.InsertBreak(BreakType.ParagraphBreak);
// Fix the alignament of the new created Paragraphs if the base Paragraph is in a list
if (clonDoc.FirstSection.Body.FirstParagraph.IsListItem)
{
clonDoc.FirstSection.Body.Paragraphs[1].ListFormat.List = null;
clonDoc.FirstSection.Body.Paragraphs[1].ParagraphFormat.StyleIdentifier = StyleIdentifier.ListParagraph;
clonDoc.FirstSection.Body.Paragraphs[1].ParagraphFormat.FirstLineIndent = 0;
}
}
clonDoc.Save("C:\\Temp\\output.docx");