Merge Paragraghs (keeping the same format)

We have a requirement to replace the text within the document which are spanned across two paragraphs, which is not allowed through Replace in Aspose (throws an exception like this: The match includes one or more special or break characters and cannot be replaced). So, we are trying to merge 2 paragraphs and then replacing the text. Is this possible? Any code snippet shall be appreciated.
Thanks,

Hi
Thanks for your inquiry. You can merge paragraphs using the following code:

// Open document
Document doc = new Document(@"Test250\in.doc");
// Get first paragraph
Paragraph firstPar = doc.FirstSection.Body.Paragraphs[0];
// Get second paragraph
Paragraph secondPar = doc.FirstSection.Body.Paragraphs[1];
// Append all children of second parafraph to first one
foreach (Node child in secondPar.ChildNodes)
{
    firstPar.AppendChild(child);
}
// Remove second paragraph
secondPar.Remove();
// Save document
doc.Save(@"Test250\out.doc");

Hope this helps.
Best regards.

Thanks for the code snippet. We are interested in replacing a paragraph which has this following text.
“\tIs this PSUR for a Centrally Authorized Product?\tYes\r\t\tNo\r\r{Q_APR} =“Yes”\r\rThis is a Paragrah 1\rSecond\rThird\rFourth\r\r\rTest\rTest1\rTest2\rTest3\r”
Is this possible

Hi
Thanks for your request. Please attach your document for testing.
Best regards.

Attached is the document.

Hi
Thanks for your inquiry. Unfortunately there is no direct way to replace several paragraphs. As a workaround you can find first paragraph and remove next paragraphs while you don’t remove all range. Then you can insert new data into the document.
Best regards.

Thanks for the response. Code snippet suggesting the workaround would be great with the earlier document sent.
Thanks

Again any code snippet is appreciated.

Hi
Thanks for your request. You can try using the following code:

public void Test258()
{
    // Open document
    Document doc = new Document(@"Test258\in.doc");
    // Create regex that will match the first paragraph of range
    Regex regex = new Regex(Regex.Escape("<%\tIs this PSUR for a Centrally Authorized Product?"));
    // execute raplace
    doc.Range.Replace(regex, new ReplaceEvaluator(Replace258), false);
    // Save result
    doc.Save(@"Test258\out.doc");
}
ReplaceAction Replace258(object sender, ReplaceEvaluatorArgs e)
{
    // Remove content
    Node currentParagraph = e.MatchNode.GetAncestor(NodeType.Paragraph);
    while (!currentParagraph.Range.Text.Contains("%>"))
    {
        currentParagraph = currentParagraph.NextSibling;
        currentParagraph.PreviousSibling.Remove();
    }
    (currentParagraph as CompositeNode).RemoveAllChildren();
    // Insert new content
    // You can use also InsertHtml method etc
    DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);
    builder.MoveTo(currentParagraph);
    builder.Write("test");
    return ReplaceAction.Skip;
}

Hope this helps.
Best regards.

Thanks for the code snippet it works like a charm. Is there a way to pass additional paramters to my ReplaceEvaluator function?

Hi
Thanks for your inquiry. I think that you would like to put replacement into ReplaceEvaluator parameters. You can try using code like the following.

public void Test258()
{
    // Open document
    Document doc = new Document(@"Test258\in.doc");
    // Create regex that will match the first paragraph of range
    Regex regex = new Regex(Regex.Escape("<%\tIs this PSUR for a Centrally Authorized Product?"));
    // execute raplace
    ReplaceParagraphs replacePar = new ReplaceParagraphs();
    replacePar.Replacement = "this is my replacement";
    doc.Range.Replace(regex, new ReplaceEvaluator(replacePar.Replace), false);
    // Save result
    doc.Save(@"Test258\out.doc");
}
class ReplaceParagraphs
{
    private string _replacement = string.Empty;
    public string Replacement
    {
        get { return _replacement; }
        set { _replacement = value; }
    }
    public ReplaceAction Replace(object sender, ReplaceEvaluatorArgs e)
    {
        // Remove content
        Node currentParagraph = e.MatchNode.GetAncestor(NodeType.Paragraph);
        while (!currentParagraph.Range.Text.Contains("%>"))
        {
            currentParagraph = currentParagraph.NextSibling;
            currentParagraph.PreviousSibling.Remove();
        }
    (currentParagraph as CompositeNode).RemoveAllChildren();
        // Insert new content
        // You can use also InsertHtml method etc
        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);
        builder.MoveTo(currentParagraph);
        builder.Write(_replacement);
        return ReplaceAction.Skip;
    }
}

Hope this helps.
Best regards.