Example: Edit a Word Document (DOCX) header in C#

In this example, we want to edit the headers of a Word Document (DOCX) file to have different formatted content.

First, we go to our Cloudmersive Account and create an API Key. Next up, we need to install the Cloudmersive Convert NuGet package:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 1.2.9

Now, let's add imports at the top of the file:

using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;

Now we will instantiate the Cloudmersive client with the API key:

// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "PASTE_YOUR_APIKEY_HERE");

var apiInstance = new EditDocumentApi();

Now we need to begin a document editing session; once we begin an editing session we can perform as many edits on the document as we want:

var inputFile = new MemoryStream(fileDocumentEditing.FileBytes); // System.IO.Stream | Input file to perform the operation on.

// Begin editing a document
string result = apiInstance.EditDocumentBeginEditing(inputFile);

Note that in this case we are creating a MemoryStream. We can of course also create a stream from a File as well. Now, let's use C# to build the content we want to put into the header:

DocxParagraph p = new DocxParagraph();
DocxRun run = new DocxRun();
DocxText text = new DocxText();
text.TextContent = "Hello, World!";
run.TextItems = new List<DocxText> { text };

DocxRun run2 = new DocxRun();
DocxText text2 = new DocxText();
text2.TextContent = "- From Cloudmersive";
run2.Bold = true;
run2.TextItems = new List<DocxText> { text2 };
                       
p.ContentRuns = new List<DocxRun> { run, run2 };

DocxHeader header = new DocxHeader();
header.Paragraphs = new List<DocxParagraph> { p };

Here, we created a paragraph, and we put multiple runs inside the paragraph. Each run then has run-level formatting (non-bold, then bold) and contains a Text content object. Now we want to submit a request to set the DOCX Header to our newly created content. Here we create and submit the edit request, which is now going to edit our document's header:

var reqConfig = new DocxSetHeaderRequest(); // DocxSetHeaderRequest | 
reqConfig.InputFileUrl = result.Replace("\"", "");

reqConfig.HeaderToApply = header;
DocxSetHeaderResponse headerResult = apiInstance.EditDocumentDocxSetHeader(reqConfig);
            

Note that one of the parameters, InputFileURL, takes the result from our first API call to begin the session - we do not need to upload the document again.

Now let's finish editing to get the result back:

var finEditing = new FinishEditingRequest(); // FinishEditingRequest | 
finEditing.InputFileUrl = headerResult.EditedDocumentURL;

byte[] finishedDocument = apiInstance.EditDocumentFinishEditing(finEditing);

Voila, we have now edited the document. Just for fun, let's convert the result to PDF so we can view it in the browser:

var convertApi = new ConvertDocumentApi();
var finishedDoc = new MemoryStream(finishedDocument); // System.IO.Stream | Input file to perform the operation on.

byte[] pdf = convertApi.ConvertDocumentDocxToPdf(finishedDoc);

800 free API calls/month, with no expiration

Get started now! or Sign in with Google

Questions? We'll be your guide.

Contact Sales