Open XML: change text in document header

Within this article I want to show how to change text in an existing document. As example the text within the document header should be modified. But you can use the same principle to change the content in all other parts of the document.

 
Find the right text element

To change the content of a text element is easy but it may be hard to find the right element. The Open XML document may have a complex structure. So it will be difficult to find the right paragraph, run and text element within this huge tree of document elements.

But there is an easy alternative. You can ignore the difficult structure and change the text directly. But this is only possible if you have a unique text element. In the next section I will show you an according example.

 
Change text in the document header

Within this example I use a template document. This document contains a header with the creation date. Of course, if I create a new document based on the template I want to set the creation date according to the actual date.

So I use a template with a unique placeholder for the date, e.g. “MY_DATE”. This unique placeholder will help me to find the right text element without the need of knowing the document structure. By using this little trick it is possible to read the document content, exchange the placeholder with the actual date and store the changes.

The following source code shows an according example. At first I create a copy of the template document. Then I open the document and read the header part. At next I exchange the placeholder with the actual date and write the changes back into the document header. At least the document changes will be stored.

//file name
string folder = @"...";
string targetFileName = folder + @"\Test.docx";
string templateFileNameTemplate = folder + @"\Template.docx";

//copy template file
if (File.Exists(targetFileName) == true)
{
    File.Delete(targetFileName);
}

File.Copy(templateFileNameTemplate, targetFileName);

//open file
using (var file = WordprocessingDocument.Open(targetFileName, true))
{
    //find the Date field within the header stream and replace it
    string content = null;

    using (StreamReader reader = new StreamReader(
        file.MainDocumentPart.HeaderParts.First().GetStream()))
    {
        content = reader.ReadToEnd();
    }

    Regex expression = new Regex("MY_DATE");
    content = expression.Replace(content, DateTime.Now.ToShortDateString());

    using (StreamWriter writer = new StreamWriter(
        file.MainDocumentPart.HeaderParts.First().GetStream(FileMode.Create)))
    {
        writer.Write(content);
    }

    //save                
    file.MainDocumentPart.Document.Save();
}
Werbung
Dieser Beitrag wurde unter .NET, C#, Open XML veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Eine Antwort zu Open XML: change text in document header

  1. Danfer Habed Lopez schreibt:

    Nice and useful article, thanks

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s