C# to replace strings of text in a docx
Asked Answered
A

3

6

Using C#, is there a good way to find and replace a text string in a docx file without having word installed on that machine?

Ailis answered 30/7, 2010 at 22:26 Comment(0)
D
5

Yes, using Open XML. Here's an article which addresses your specific question: Creating a Simple Search and Replace Utility for Word 2007 Open XML Format Documents

To work with this file format, one option is to use the Open XML Format Application Programming Interface (API) in the DocumentFormat.OpenXml.Packaging namespace. The classes, methods, and properties in this namespace are located in the DocumentFormat.OpenXml.dll file. You can install this DLL file by installing the Open XML Format SDK version 1.0. The members in this namespace allow you to easily work with the package contents for Excel 2007 workbooks, PowerPoint 2007 presentations, and Word 2007 documents.

...

Private Sub Search_Replace(ByVal file As String)
Dim wdDoc As WordprocessingDocument = WordprocessingDocument.Open(file, True)

' Manage namespaces to perform Xml XPath queries.
Dim nt As NameTable = New NameTable
Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(nt)
nsManager.AddNamespace("w", wordmlNamespace)

' Get the document part from the package.
Dim xdoc As XmlDocument = New XmlDocument(nt)
' Load the XML in the part into an XmlDocument instance.
xdoc.Load(wdDoc.MainDocumentPart.GetStream)

' Get the text nodes in the document.
Dim nodes As XmlNodeList = Nothing
nodes = xdoc.SelectNodes("//w:t", nsManager)
Dim node As XmlNode
Dim nodeText As String = ""

' Make the swap.
Dim oldText As String = txtOldText.Text
Dim newText As String = txtNewText.Text
For Each node In nodes
   nodeText = node.FirstChild.InnerText
   If (InStr(nodeText, oldText) > 0) Then
      nodeText = nodeText.Replace(oldText, newText)
      ' Increment the occurrences counter.
      numChanged += 1
   End If
Next

' Write the changes back to the document.
xdoc.Save(wdDoc.MainDocumentPart.GetStream(FileMode.Create))

' Display the number of change occurrences.
txtNumChanged.Text = numChanged
End Sub
Doubledecker answered 30/7, 2010 at 22:29 Comment(4)
Thanks that definitely got me started. It looks like it is all based on System.IO.Packaging. Since this is fairly simple, can it be done without the Open XML Format SDK?Ailis
Absolutely - I rarely use the SDK myself. I primarily program against PowerPoint (PresentationML and DrawingML as opposed to Word's WordProcessingML) using only System.IO.Packaging and Linq-to-XML. So I'll have to point you to a Ken Getz article: msdn.microsoft.com/en-us/library/bb738371(office.12).aspx. Look for any more of his articles written in 2006 - they all use System.IO.Packaging. After that, he started writing articles with the SDK. You can also check out openxmldeveloper.orgDoubledecker
Awesome, thank you. Also, this reference by Vikas Goyal helped me tremendously both in getting the answer I needed and in (mostly) understanding what was going on in the process: devx.com/dotnet/Article/42221/1954Ailis
@timothyawiseman: the devx article is a really good one. glad to hear this is working out for you.Doubledecker
R
0

You may also try Aspose.Words for .NET in order to find and replace text in Word document. This component doesn't require MS Office to be installed. The API is quite simple and easy to use and implement.

Disclosure: I work as developer evangelist at Aspose.

Rivalry answered 23/8, 2011 at 16:48 Comment(0)
L
0

Or you might try DocxTemplater. An open source library. Not as sophisticated as Aspose but open source. https://github.com/Amberg/DocxTemplater

Lingenfelter answered 11/1 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.