Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenXml Edit text in the header of a word file

I'm using Open XML and I should change the text in the header of a word file. To change a specific paragraph in the document I have used the following code:

Dim body = wdDoc.MainDocumentPart.Document.Body
            Dim paras = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
            Dim header = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Header)()


            For Each para In paras
                For Each run In para.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
                    For Each testo In run.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
                        If (testo.Text.Contains("<$doc_description$>")) Then
                            testo.Text = testo.Text.Replace("<$doc_description$>", "replaced-text")
                        End If
                    Next
                Next
            Next

thanks in advance!

like image 442
invictus1306 Avatar asked Sep 25 '13 14:09

invictus1306


2 Answers

Ported to C# from Hans answer!

 //Gets all the headers
 foreach (var headerPart in doc.MainDocumentPart.HeaderParts)
 {
      //Gets the text in headers
      foreach(var currentText in headerPart.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>())
      {
          currentText.Text = currentText.Text.Replace("[Thanks]", "Thanks");
      }
 }
like image 189
BrunoMartinsPro Avatar answered Sep 19 '22 20:09

BrunoMartinsPro


You can use the following code to replace a tag in the header of a word document:

Using wdDoc = WordprocessingDocument.Open("header.docx", True)

  For Each headerPart In wdDoc.MainDocumentPart.HeaderParts
    For Each currentParagraph In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
      For Each currentRun In currentParagraph.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
        For Each currentText In currentRun.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()

          If (currentText.Text.Contains("$doc-description$")) Then
            Console.WriteLine("found")
            currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text")
          End If

        Next
      Next
    Next
  Next

End Using

First, enumerate all HeaderParts of the word document. Then search for all Text elements containing the tag to replace. Then replace the tag with your text.

Please note that you should use a tag without <> and _ characters. If your tag contains these characters then word splits the text among multiple Text elements.

If you want to change the text in a table (or in any other element) just search for all Text elements:

Using wdDoc = WordprocessingDocument.Open("header.docx", True)

  For Each headerPart In wdDoc.MainDocumentPart.HeaderParts
    For Each currentText In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()

      If (currentText.Text.Contains("$doc-description$")) Then
        Console.WriteLine("found")
        currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text")
      End If

    Next
  Next

End Using
like image 26
Hans Avatar answered Sep 20 '22 20:09

Hans