How to insert content into a Word document using C#, VB.NET
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 into a Word document using C# and VB.NET.
Steps to insert content into a Word document programmatically using C#:
- Create a new C# console application project.
- Install the Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.DocIO.DLS; using System.Drawing;
VB
Imports Syncfusion.DocIO.DLS Imports System.Drawing
- Use the following code snippet to insert content into a 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 the margin of the section.
section.PageSetup.Margins.All = 72;
// Adds a 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 a 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 a 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 a new paragraph to the section.
paragraph = section.AddParagraph();
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
paragraph.ParagraphFormat.BeforeSpacing = 12f;
// Appends a picture to the paragraph.
WPicture picture = (WPicture)paragraph.AppendPicture(Image.FromFile(@"..\..\Images\Mountain-200.png"));
// Adds a new paragraph to the section.
paragraph = section.AddParagraph();
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
paragraph.ParagraphFormat.BeforeSpacing = 12f;
// Appends a 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 the margin of the section.
section.PageSetup.Margins.All = 72
' Adds a 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 a 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 a 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 a new paragraph to the section.
paragraph = section.AddParagraph()
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center
paragraph.ParagraphFormat.BeforeSpacing = 12.0F
' Appends a picture to the paragraph.
Dim picture As WPicture = CType(paragraph.AppendPicture(Image.FromFile("..\..\Images\Mountain-200.png")), WPicture)
' Adds a new paragraph to the section.
paragraph = section.AddParagraph()
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center
paragraph.ParagraphFormat.BeforeSpacing = 12.0F
' Appends a 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.
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 into a Word document.
See Also:
Create Word document in Windows Forms
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
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.