Move Cursor to Specific Paragraph | Insert Paragraph with Same Style of Current Paragraph Style using .NET

Hi aspose team.

How can I copy a paragraph with all the styles and add it next to the original in the same document?
Basically, I want to duplicate the paragraph on the document.

Kind regards,
Paulo Pereira

@paulo_pereira

Please use the following code example to copy the paragraph with styles and insert it to same document.

Document doc = new Document(MyDir + "input.docx");
Paragraph paragraph = (Paragraph)doc.GetChild(NodeType.Paragraph, 0, true);
Paragraph newPara = (Paragraph)paragraph.Clone(true);

paragraph.ParentSection.Body.InsertAfter(newPara, paragraph);
doc.Save(MyDir + "output.docx"); 

Hi @tahir.manzoor,
Thank you for the feedback.
Another question,
I’m at the first paragraph with the Document Builder, how can I add a new paragraph right after the current with the same styles?

I tried to move to the end of the current paragraph and then use the builder.writeln(“new text”), but the “new text” was written before the current paragraph instead.

Kind regards,
Paulo

@paulo_pereira

Please use DocumentBuilder.MoveToParagraph method to move the cursor to the paragraph. You can use the value of second parameter as -1 in this method to move the cursor to the end of the paragraph. Please read the following article.

Following code example shows how to move the cursor to the end of paragraph and insert some text.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
ParagraphCollection paragraphs = doc.FirstSection.Body.Paragraphs;

Paragraph paragraph = doc.FirstSection.Body.Paragraphs[1];
builder.MoveTo(paragraph);

builder.MoveToParagraph(paragraphs.IndexOf(builder.CurrentParagraph), -1);
builder.Writeln("New Paragraph");

doc.Save(MyDir + "output.docx");