How to set the company name and logo in the header of a Word document in C#?
Syncfusion® Essential® DocIO is a .NET Core Word library used to programmatically create, read, and edit Word documents without Microsoft Word or Interop dependencies. Using this library, set the company name and logo in the header of a Word document in C#.
Steps to set the company name and logo in the header of a Word document in C#
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package to reference 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 set the company name and logo in the header of a Word document.
C#
//Open an existing Word document. using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) { //Get the Word document section. IWSection section = document.Sections[0]; //Add a paragraph to the header. IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph(); // Get the image stream. FileStream imageStream = new FileStream(@"../../../Data/Logo.jpg", FileMode.Open, FileAccess.Read); //Append the picture to the created paragraph. IWPicture picture = paragraph.AppendPicture(imageStream); //Set the picture properties. picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText; picture.VerticalOrigin = VerticalOrigin.Margin; picture.VerticalPosition = -45; picture.HorizontalOrigin = HorizontalOrigin.Column; picture.HorizontalPosition = 263.5f; picture.WidthScale = 20; picture.HeightScale = 15; //Append the text to the created paragraph. WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange; //Apply formatting for the text range. paragraph.ApplyStyle("Normal"); textRange.CharacterFormat.FontSize = 12f; textRange.CharacterFormat.FontName = "Calibri"; textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red; paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Save a Word document to file stream. document.Save(outputFileStream, FormatType.Docx); } } }
A complete working sample example to set the company name and logo in the header of 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, a PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
See Also:
How to insert headers and footers in a document
Conclusion
I hope you enjoyed learning about how to set the company name and logo in the header of a Word document in C#.
You can refer to our .NET Core Word library 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 .NET Core Word library example to understand how to create and manipulate data.
For current customers, you can check out our Document Processing Libraries from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-trac, or feedback portal. We are always happy to assist you!