Articles in this section
Category / Section

How to insert a row with the same formatting in a Word document?

5 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can insert a row with the same formatting in a Word document.

Steps to insert a row with the same formatting using C#:

  1. Create a new C# console application project.

Create Console application in Visual Studio

  1. Install  Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.

Add DocIO NuGet package reference to the project

  1. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

VB

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
  1. Use the following code example to insert a row with the same formatting in the table

C#

//Open an existing Word document.
using (WordDocument document = new WordDocument("Sample.docx"))
{
    //Get the table from a Word document.
    WTable table = document.Sections[0].Tables[0] as WTable;
    //Clone the row.
    WTableRow row = table.Rows[0].Clone();
    //Iterate all cells in a row and clear the contents.
    for (int i = 0; i < row.Cells.Count; i++)
    {
        WTableCell tableCell = row.Cells[i];
        tableCell.ChildEntities.Clear();
    }
    //Insert a new paragraph to the first cell.
    WParagraph cellParagraph = row.Cells[0].AddParagraph() as WParagraph;
    //Set text to the paragraph.
    IWTextRange textRange = cellParagraph.AppendText("New row's first cell");
    //Insert a row into the table in a specific index.
    table.Rows.Insert(2, row);
    //Save a Word document.
    document.Save("Result.docx", FormatType.Docx);
}

VB

'Open an existing Word document.
Using document As WordDocument = New WordDocument("Sample.docx")
    'Get the table from a Word document.
    Dim table As WTable = CType(document.Sections(0).Tables(0), WTable)
    'Clone the row.
    Dim row As WTableRow = table.Rows(0).Clone
    Dim i As Integer = 0
    'Iterate all cells in a row and clear the contents.
    Do While (i < row.Cells.Count)
        Dim tableCell As WTableCell = row.Cells(i)
        tableCell.ChildEntities.Clear()
        i = (i + 1)
    Loop
    'Insert a new paragraph to the first cell.
    Dim cellParagraph As WParagraph = CType(row.Cells(0).AddParagraph, WParagraph)
    'Set text to the paragraph.
    Dim textRange As IWTextRange = cellParagraph.AppendText("New row's first cell")
    'Insert a row into the table in a specific index.
    table.Rows.Insert(2, row)
    'Save a Word document.
    document.Save("Result.docx", FormatType.Docx)
End Using

A complete working example to insert a row with the same formatting in a Word document can be downloaded from GitHub.

By executing the application, you will get the output Word document as follows.

Output Word document generated in WinForms

Take a moment to peruse the documentation . Find basic Word document processing options along with features like mail merge, merge, and split documents, find and replace text in a Word document, protect the Word documents, and most importantly, PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without a trail message.




Conclusion

I hope you enjoyed learning how to insert a row with the same formatting in a Word document.

You can refer to our WinForms Word feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Word example to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms Edit control and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied