Articles in this section
Category / Section

How to insert content in Word document using C#, VB.NET

3 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 content in Word document using C# and VB.NET.

Steps to insert content in Word document programmatically 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 the NuGet.org.

Add DocIO NuGet package reference to the project

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

C#

using Syncfusion.DocIO.DLS;
using System.Drawing;

VB

Imports Syncfusion.DocIO.DLS
Imports System.Drawing
  1. Use the following code snippet to insert content in Word document with text and images.

C#

//Creates a new Word document.
using (WordDocument document = new WordDocument())
{
    //Adds a new section to the document.
    IWSection section = document.AddSection();
    //Sets margin of the section.
    section.PageSetup.Margins.All = 72;
    //Adds new paragraph to the section.
    IWParagraph paragraph = section.AddParagraph();
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
    paragraph.ApplyStyle(BuiltinStyle.Heading1);
    //Adds new text to the paragraph.
    WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
    textRange.CharacterFormat.FontSize = 18f;
    textRange.CharacterFormat.FontName = "Calibri";
    //Adds new paragraph to the section.
    paragraph = section.AddParagraph();
    paragraph.ParagraphFormat.FirstLineIndent = 36;
    paragraph.BreakCharacterFormat.FontSize = 12f;
    //Adds new text to the paragraph.
    textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange;
    textRange.CharacterFormat.FontSize = 12f;
    //Adds new paragraph to the section.
    paragraph = section.AddParagraph();
    paragraph.ApplyStyle(BuiltinStyle.Heading1);
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
    //Adds new text to the paragraph.
    textRange = paragraph.AppendText("Product Overview") as WTextRange;
    textRange.CharacterFormat.FontSize = 16f;
    textRange.CharacterFormat.FontName = "Calibri";
    //Adds new paragraph to the section.
    paragraph = section.AddParagraph();
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
    paragraph.ParagraphFormat.BeforeSpacing = 12f;
    //Appends picture to the paragraph.
    WPicture picture = (WPicture)paragraph.AppendPicture(Image.FromFile(@"..\..\Images\Mountain-200.png"));
    //Adds new paragraph to the section.
    paragraph = section.AddParagraph();
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
    paragraph.ParagraphFormat.BeforeSpacing = 12f;
    //Appends picture to the paragraph.
    picture = (WPicture)paragraph.AppendPicture(Image.FromFile(@"..\..\Images\Road-550-W.jpg"));
    //Saves the Word document.
    document.Save("Result.docx");
}

VB

'Creates a new Word document.
Using document As WordDocument = New WordDocument()
    'Adds a new section to the document.
    Dim section As IWSection = document.AddSection()
    'Sets margin of the section.
    section.PageSetup.Margins.All = 72
    'Adds new paragraph to the section.
    Dim paragraph As IWParagraph = section.AddParagraph()
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center
    paragraph.ApplyStyle(BuiltinStyle.Heading1)
    'Adds new text to the paragraph.
    Dim textRange As WTextRange = TryCast(paragraph.AppendText("Adventure Works Cycles"), WTextRange)
    textRange.CharacterFormat.FontSize = 18.0F
    textRange.CharacterFormat.FontName = "Calibri"
    'Adds new paragraph to the section.
    paragraph = section.AddParagraph()
    paragraph.ParagraphFormat.FirstLineIndent = 36
    paragraph.BreakCharacterFormat.FontSize = 12.0F
    'Adds new text to the paragraph.
    textRange = TryCast(paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base."), WTextRange)
    textRange.CharacterFormat.FontSize = 12.0F
    'Adds new paragraph to the section.
    paragraph = section.AddParagraph()
    paragraph.ApplyStyle(BuiltinStyle.Heading1)
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center
    'Adds new text to the paragraph.
    textRange = TryCast(paragraph.AppendText("Product Overview"), WTextRange)
    textRange.CharacterFormat.FontSize = 16.0F
    textRange.CharacterFormat.FontName = "Calibri"
    'Adds new paragraph to the section.
    paragraph = section.AddParagraph()
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center
    paragraph.ParagraphFormat.BeforeSpacing = 12.0F
    'Appends picture to the paragraph.
    Dim picture As WPicture = CType(paragraph.AppendPicture(Image.FromFile("..\..\Images\Mountain-200.png")), WPicture)
    'Adds new paragraph to the section.
    paragraph = section.AddParagraph()
    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center
    paragraph.ParagraphFormat.BeforeSpacing = 12.0F
    'Appends picture to the paragraph.
    picture = CType(paragraph.AppendPicture(Image.FromFile("..\..\Images\Road-550-W.jpg")), WPicture)
    'Saves the Word document.
    document.Save("Result.docx")
End Using

A complete working example to insert content in Word document using C# can be downloaded from Insert-content-in-Word-document.zip

By executing the application, you will get the output Word document as follows. Inserted content in Word document in C# using DocIO

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the 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.

An online example to insert content in Word document.

See Also:

Create Word document in Windows Forms

Create Word document in WPF

Create Word document in ASP.NET Core

Create Word document in ASP.NET MVC

Create Word document in Xamarin

Create Word document in Xamarin.Android

Note:

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

 

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