【问题标题】:Insert a content control with OOXML使用 OOXML 插入内容控件
【发布时间】:2020-07-30 20:53:30
【问题描述】:

docs推荐以下方式添加内容控件:

<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
  <pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
    <pkg:xmlData>
      <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
        <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
      </Relationships>
    </pkg:xmlData>
  </pkg:part>
  <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
    <pkg:xmlData>
      <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
        <w:body>
          <w:p/>
          <w:sdt>
              <w:sdtPr>
                <w:alias w:val="MyContentControlTitle"/>
                <w:id w:val="1382295294"/>
                <w15:appearance w15:val="hidden"/>
                <w:showingPlcHdr/>
              </w:sdtPr>
              <w:sdtContent>
                <w:p>
                  <w:r>
                  <w:t>[This text is inside a content control that has its container hidden. You can bind to a content control to add or interact with content at a specified location in the document.]</w:t>
                </w:r>
                </w:p>
              </w:sdtContent>
            </w:sdt>
          </w:body>
      </w:document>
    </pkg:xmlData>
  </pkg:part>
 </pkg:package>

但如果仔细观察,内容控件会添加到段落&lt;w:p&gt;)中:

<w:sdtContent>
  <w:p>
    <w:r>
      <w:t>[This text is inside a content control that has its container hidden. You can bind to a content control to add or interact with content at a specified location in the document.]</w:t>
    </w:r>
  </w:p>
</w:sdtContent>

有没有办法可以防止在段落中添加它并使其内联?仅删除 &lt;w:p&gt; 标记不起作用。此外,我熟悉用于添加内容控件的 Word API。我的情况是,我必须生成 OOXML,然后插入到文档中。

【问题讨论】:

    标签: ms-word office-js openxml


    【解决方案1】:

    如果您从&lt;w:sdtContent&gt; 元素中删除&lt;w:p&gt;&lt;/w:p&gt;,则必须像这样将 元素包装在&lt;w:p&gt; 元素中,否则Word 将无法识别内容:

    <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
      <pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
        <pkg:xmlData>
          <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
            <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
          </Relationships>
        </pkg:xmlData>
      </pkg:part>
      <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
        <pkg:xmlData>
          <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
            <w:body>
              <w:p/>
              <w:p>
                <w:sdt>
                    <w:sdtPr>
                      <w:alias w:val="MyContentControlTitle"/>
                      <w:id w:val="1382295294"/>
                      <w15:appearance w15:val="hidden"/>
                      <w:showingPlcHdr/>
                    </w:sdtPr>
                    <w:sdtContent>
                      <w:r>
                        <w:t>[This text is inside a content control that has its container hidden. You can bind to a content control to add or interact with content at a specified location in the document.]</w:t>
                      </w:r>
                    </w:sdtContent>
                  </w:sdt>
                </w:p>
              </w:body>
          </w:document>
        </pkg:xmlData>
      </pkg:part>
     </pkg:package>
    

    在 Word 中查看,在“您的”编码样式中,段落标记出现在内容控件内。使用上述编码,它出现在内容控件之外,这意味着可以在控件末尾和段落末尾之间插入内容。

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多