2011-06-20 62 views
1

使用Open XML SDK我已成功地以編程方式在Word文檔中查找書籤或文本字符串並插入新內容。我正在使用OpenXmlPowerTools.SearchAndReplacer來做文本搜索和替換,並且這篇文章的書籤答案爲Replace bookmark text in Word file using Open XML SDK使用OpenXML替換Word 2010文本框中的內容

當我試圖替換的書籤或文本位於文本框內時,這一切都會失敗。

爲什麼兩種方法都不能在文本框中工作? Word文檔我試圖替換使用Texboxes內部的內容進行佈局,我無法弄清楚問題所在。

有沒有人有什麼可能是問題的建議?由於

+0

一個DOCX文檔是basicly一個XML文檔你提到可能搜索的方法對於將文本作爲內容的正確類型的標籤,文本框可能會有一個完全不同的標籤,而您的代碼會忽略它,我會用TextBox檢查一個文檔的基礎XML並查看它是如何保存在那裏的。 –

回答

1

這裏是它的一個簡單的文本框以單詞測試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> 

你可以看到結構是不同的,那麼尋找一個書籤中的文本時,因爲一個文本框作爲一個實際存儲圖片。如果您調整搜索算法以處理這種不同的結構,那麼您應該能夠找到文本並將其替換。

2

我這樣做 - 。它適用於文本框的情況下有沒有用文字多次運行(如1個字加粗

​​
相關問題