docx unbreakable words

前端 未结 1 1694
时光说笑
时光说笑 2021-01-23 02:35

I\'m trying to replace words in a docx file like described here:

public static void SearchAndReplace(string document)
{
    using (WordprocessingDocument wordDoc         


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-23 03:13

    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.

    0 讨论(0)
提交回复
热议问题