Skip to main content Skip to footer

Load, Modify, and Save Word Documents in .NET Standard Applications

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:

  • Need to update the company logo in the header of each page in your Word document
  • Need to update the data in a financial budget report
  • Need to add Date/Time to an article
  • Need to update a document your team member has created
  • Need to change an official letter template.

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.

Use case for modifying Word documents

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.

Step 1: Load existing Word document

In your application, load the existing Word document in GcWord object.

GcWordDocument doc=new GcWordDocument();
doc.Load(@"ImportanceOfWetlands.docx");

Step 2: Add a header to the document

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];

Step 3: Add company logo to the header

You may want to add the following company logo along with the company name: Acme Inc.

Load, Modify, and Save Word documents in .NET Standard Applications

  1. Any object in a GcWord document can be modified via Runs. Add an additional run to a range of runs obtained from the header paragraph:
RunCollection runs = p.GetRange().Runs;
byte[] bytes = File.ReadAllBytes(@"acme.png");
Picture pic = runs.Add().GetRange().Pictures.Add(bytes, "image/png");
  1. Adjust the picture size:
pic.Size.Width.Value = 76.05f;
pic.Size.Height.Value = 19.45f;

Step 4: Add company name text on the right side of the header

runs.Add().GetRange().Texts.Add("\t\tAcme Inc.");

Step 5: Save the Word document back

doc.Save(@"ImportanceOfWetlands.docx");

Run the application, and your Word document will look like this:

Load, Modify, and Save Word documents in .NET Standard Applications

Modify the Word document and add pictures

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[4].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:

Modifying Word document and adding pictures

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.

Download the complete sample here.

Try out more GcWord demos.

Shilpa Sharma - Product Manager

Shilpa Sharma

Product Manager
comments powered by Disqus