How to insert author's name in a Word document?
Syncfusion® Essential® DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can insert author’s name in a Word document in C#.
Steps to insert author’s name in a Word document:
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
- Use the following code example to insert author’s name in a Word document.
C#
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
//Open the template document.
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
{
//Get the section in a Word document.
IWSection section = document.LastSection;
//Add paragraph to the document section.
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Author: ");
//Add field to represent Author from document properties.
paragraph.AppendField("Author", FieldType.FieldDocProperty);
//Update the fields in Word document.
document.UpdateDocumentFields();
//Create file stream.
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the Word document to file stream.
document.Save(outputStream, FormatType.Docx);
}
}
}
A complete working sample to insert author’s name in a Word document in C# can be downloaded from GitHub.
By executing the program, you will get the Output document as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
See Also:
Is it possible to update Include Text field in Word document using DocIO?