Document Solutions for Word
Features / Paragraph / Paragraph
In This Topic
    Paragraph
    In This Topic

    DsWord allows you to add, modify, and delete a paragraph from a Word document. It allows you to add a paragraph to the paragraph collection using Add method of the ParagraphCollection class, modify it using the Paragraphs property which accesses the paragraph collection, and delete it using the Delete method of the ContentObject class.

    Add Paragraph

    To add a paragraph to a document:

    1. Access the paragraph collection using Paragraphs property of the RangeBase class.
    2. Add a paragraph using Add method of the ParagraphCollection class.
      C#
      Copy Code
      //Add first paragraph
      doc.Body.Sections.First.GetRange().Paragraphs.Add("Hello, World!");
      
      //Add another paragraph
      doc.Body.Sections.First.GetRange().Paragraphs.Add("Document Solutions");
      
      //Save the document
      doc.Save("AddParagraph.docx"); 
    Back to Top

    Modify Paragraph

    To modify a paragraph in a document:

    1. Access the paragraph you want to modify from the paragraph collection using Paragraphs property of the RangeBase class. For example, access the first paragraph using First property of the ParagraphCollection class.
    2. Modify the Paragraph class properties. For example, use the Format property for accessing the paragraph formatting properties such as the Alignment property to set the alignment of the paragraph.
      C#
      Copy Code
      doc.Load("AddParagraph.docx");
      //Modify settings of the first paragraph
      doc.Body.Sections.First.GetRange().Paragraphs.First.Format.Alignment = ParagraphAlignment.Center;
      
      //Save the document
      doc.Save("ModifyParagraph.docx");
    Back to Top

    Delete Paragraph

    To delete a paragraph from a document, access a paragraph from the paragraph collection using the Paragraphs property and delete it using Delete method of the ContentObject class.

    C#
    Copy Code
    doc.Load("AddParagraph.docx");
    //Delete the last paragraph
    doc.Body.Sections.First.GetRange().Paragraphs.Last.Delete();
    
    //Save the document
    doc.Save("DeleteParagraph.docx");
    Back to Top

    For more information on how to work with paragraphs using DsWord, see DsWord sample browser.