Add Dynamic paragraph break between two para using Aspose word Dot net

Hi Team,

We have a requirement to add dynamic paragraph break between content.

Say we have a content as {First para}.{Second para} then we need to add a dynamic paragraph break before {Second para} and the result would be as
{First para}.

{Second para}

One point to be noted is that there needs to be para spacing between two para as well. Kindly let us know if this is feasible through Aspose word.

Thanks,
Karthikeyan

@Karthik_Test_account can you please post a file example for input and expected output.

@Eduardo_Canal Please find the Input and the expected output file below.

Input File: Parabreak_Input.docx (15.3 KB)

Output File: Parabreak_out.docx (25.8 KB)

@Karthik_Test_account thanks for the additional information. The simplest way to achieve what you want is just inserting a line break in the text:

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

// Inserting line break
var text = clonDoc.FirstSection.Body.FirstParagraph.Range.Replace("}{", "}.\r{");
clonDoc.Save("C:\\Temp\\output.docx");

output.docx (10.8 KB)

@Eduardo_Canal - After splitting is there a way to add paragraph after spacing points for the first para such that there is a spacing between two paragraphs.

@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");