Not all documents are created from scratch with a Word API or any other document API. Many use cases revolve around using existing Word documents and modifying them with updated data and saving them back.
You may need to modify existing Word documents in following examples:
GrapeCity Documents for Word (GcWord) is a .NET Standard 2.0 targeted Word API that can create, load, modify, and save existing Word documents to docx or PDF format.
Learn about GcWord's features and supported formats.
Let's go through one of the use cases in detail and see how we can solve it using GcWord.
As a media company, the company publishes several articles on social issues every month on the web. Several writers within the company, or freelancers working for the company, send articles to the marketing team for review each day. Also, because logos are considered confidential, they are not circulated to everyone within or outside of the company.
The marketing team wants to add a company logo and name on the header of each page in each article. Since there are a large number of articles received every week, the team cannot add the logo and company name on each article manually. Also, due to marketing strategies, the logo of the company may undergo changes from time to time. Thus, the marketing team wants to make this job an automated process. A team of software developers is hired to do this.
This blog takes the simple problem from above and guides you through steps to load, modify, and save Word documents in a .NET Core console application. In this example, we will take an existing Word document and add a company name and logo on the header of the Word document using GcWord.
We'll also show you how to add an image in-between the document content using GcWord.
In your application, load the existing Word document in GcWord object.
GcWordDocument doc=new GcWordDocument(); doc.Load(@"ImportanceOfWetlands.docx");
Add a paragraph to the header of the first section and set the style to the first section:
Paragraph p = doc.Body.Sections.First.Headers[HeaderFooterType.Primary].Body.Paragraphs.Add(); p.Style = doc.Styles[BuiltInStyleId.Header];
You may want to add the following company logo along with the company name: Acme Inc.
RunCollection runs = p.GetRange().Runs; byte bytes = File.ReadAllBytes(@"acme.png"); Picture pic = runs.Add().GetRange().Pictures.Add(bytes, "image/png");
pic.Size.Width.Value = 76.05f; pic.Size.Height.Value = 19.45f;
Run the application, and your Word document will look like this:
You can add an image in-between the document's content blocks. Add the following code to add a picture after the 5th paragraph in the document:
p = doc.Body.Paragraphs.GetRange().Paragraphs.Insert(InsertLocation.After); p.Style = doc.Styles["par style 1"]; bytes = File.ReadAllBytes(@"birds.png"); pic = p.GetRange().Runs.Add().GetRange().Pictures.Add(bytes, "image/png"); pic.Size.Width.Value = 381.85f; pic.Size.Height.Value = 217.2f;
Save the document again (as in Step 5 above) and you will see the picture added in your Word document:
That's how you can modify Word documents using GcWord. For more samples and tutorials about the GrapeCity Documents for Word API, check out how you can generate Word documents in code and how to build a .NET Core console application with GcWord in Visual Studio for Windows, MAC, and Linux.