I\'m trying to replace words in a docx file like described here:
public static void SearchAndReplace(string document)
{
using (WordprocessingDocument wordDoc
One way to solve this is normalizing the xml of your document before doing transformtions. You can make use of OpenXml Powertools to do this.
Sample code to normalize xml
using (WordprocessingDocument doc =
WordprocessingDocument.Open("Test.docx", true))
{
SimplifyMarkupSettings settings = new SimplifyMarkupSettings
{
NormalizeXml = true, // Merges Run's in a paragraph with similar formatting
// Additional settings if required
AcceptRevisions = true,
RemoveBookmarks = true,
RemoveComments = true,
RemoveGoBackBookmark = true,
RemoveWebHidden = true,
RemoveContentControls = true,
RemoveEndAndFootNotes = true,
RemoveFieldCodes = true,
RemoveLastRenderedPageBreak = true,
RemovePermissions = true,
RemoveProof = true,
RemoveRsidInfo = true,
RemoveSmartTags = true,
RemoveSoftHyphens = true,
ReplaceTabsWithSpaces = true
};
MarkupSimplifier.SimplifyMarkup(doc, settings);
}
This will simplify the markup of Open Xml document to make further transformations easier to work with the document programatically. I always use it before working with a open xml document programatically.
More Info about using these tools can be found here and a good blog article here.