简体   繁体   English

使用OpenXML替换Word 2010文本框中的内容

[英]Replacing content in Word 2010 Textboxes using OpenXML

Using the Open XML SDK I've been successful in programatically finding bookmarks or text strings in a word document and inserting new content. 使用Open XML SDK,我已经成功地以编程方式在Word文档中查找书签或文本字符串并插入了新内容。 I'm using OpenXmlPowerTools.SearchAndReplacer to do the text search and replace and this post's answer for the bookmarks Replace bookmark text in Word file using Open XML SDK 我正在使用OpenXmlPowerTools.SearchAndReplacer进行文本搜索和替换,而本文的书签答案是使用Open XML SDK替换Word文件中的书签文本。

This all fails when the bookmark or the text I am trying to replace is located inside a Textbox. 当书签或我要替换的文本位于文本框内时,所有操作均将失败。

Why does neither approach work within a Textbox? 为什么这两种方法都无法在文本框中使用? The Word documents I am trying to replace content within use Texboxes for layout and I can't work out what the problem is. 我试图替换使用Texboxes中的内容进行布局的Word文档,但我无法解决问题所在。

Does anybody have suggestions as to what might be the problem? 有人对可能出现的问题有任何建议吗? Thanks 谢谢

I did this - it works on text boxes in the case where there are not multiple runs with text (like 1 word bolded 我这样做-在文本不存在多次运行的情况下,它适用于文本框(例如1字加粗)

    Dim searchQuery = From tx In mainPart.Document.Body.Descendants(Of Text)()
        Where tx.Text.Contains(replaceData.OldText)

    Dim i As Integer
    For i = 0 To searchQuery.Count - 1
        searchQuery(i).Text = searchQuery(i).Text.Replace(replaceData.OldText, replaceData.NewText)
    Next

Here is the XML for a simple textbox with the word test in it: 这是其中包含单词test的简单文本框的XML:

<w:pict xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
    <v:stroke joinstyle="miter" />
    <v:path gradientshapeok="t" o:connecttype="rect" />
  </v:shapetype>
  <v:shape id="_x0000_s1027" style="position:absolute;margin-left:0;margin-top:0;width:186.35pt;height:110.6pt;z-index:251660288;mso-width-percent:400;mso-height-percent:200;mso-position-horizontal:center;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin" type="#_x0000_t202" xmlns:v="urn:schemas-microsoft-com:vml">
    <v:textbox style="mso-fit-shape-to-text:t">
      <w:txbxContent>
        <w:p w:rsidR="00B558B5" w:rsidRDefault="00B558B5">
          <w:proofErr w:type="gramStart" />
          <w:r>
            <w:t>test</w:t>
          </w:r>
          <w:proofErr w:type="gramEnd" />
        </w:p>
      </w:txbxContent>
    </v:textbox>
  </v:shape>
</w:pict>

You can see the structure is different then when searching for text within a bookmark since a textbox is actually stored as a picture. 您可以看到结构与在书签中搜索文本时有所不同,因为文本框实际上是作为图片存储的。 If you adjust your searching algorithm to deal with this different structure then you should be able to find the text and replace it. 如果您调整搜索算法以处理这种不同的结构,那么您应该能够找到文本并将其替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM