编辑 MS Word 中的 “Document Info” 字段在 Sdt Content 中,使用 docx4j。

huangapple go评论133阅读模式
英文:

Editing MS Word "Document Info" field in Sdt Content by docx4j

问题

抱歉,我只能提供中文翻译,不能提供代码部分的翻译。

你想要使用docx4j更改Sdt Content的内容,但在XML头文件中尝试更改<w:t>值时,生成的文档中该值没有更新。编辑不包含Sdt Block的其他<w:t>元素时,一切正常。

你想要知道如何更新包含“文档信息”字段的Std Content中的文本吗?源文件中的标题和第一页字段如何相互关联(它们始终显示相同的值)?

英文:

I have a template of a docx document and using docx4j I would like to change the content of Sdt Content. The Sdt Block contains the title of the document from the MS Word "Document Info" field, which is displayed in the header and on the first page.
Unfortunately, when I try to change the &lt;w:t&gt; value in &lt;w:sdtContent&gt; in the XML header file, the value does not update in the generated document. Editing other &lt;w:t&gt; elements without Sdt Block works fine for me.

I would like to ask how can I update the text in Std Content which contains "Document Info" field?
How are these header and first page fields with title related to each other in the source files (they always display the same values)?

答案1

得分: 1

以下是翻译好的内容:

这样的SDT看起来会像这样:

&lt;w:sdt&gt;
  &lt;w:sdtPr&gt;
    &lt;w:alias w:val=&quot;标题&quot;/&gt;
    &lt;w:dataBinding w:prefixMappings=&quot;xmlns:ns0=&#39;http://purl.org/dc/elements/1.1/&#39; xmlns:ns1=&#39;http://schemas.openxmlformats.org/package/2006/metadata/core-properties&#39; &quot; w:xpath=&quot;/ns1:coreProperties[1]/ns0:title[1]&quot; w:storeItemID=&quot;{6C3C8BC8-F283-45AE-878A-BAB7291924A1}&quot;/&gt;
    &lt;w:text/&gt;
  &lt;/w:sdtPr&gt;
  &lt;w:sdtContent&gt;
    &lt;w:p&gt;
      &lt;w:r&gt;
        &lt;w:t&gt;我的标题&lt;/w:t&gt;
      &lt;/w:r&gt;
    &lt;/w:p&gt;
  &lt;/w:sdtContent&gt;
&lt;/w:sdt&gt;

注意数据绑定元素;具有属性w:xpath="/ns1:coreProperties[1]/ns0:title[1]"。

Word将尝试更新具有数据绑定元素的任何SDT,从其对应的存储中获取(由w:storeItemID指定)。(也就是说,只有在没有数据绑定元素的情况下,才能像您描述的那样更新文本)

在这种情况下,它将使用/docProps/core.xml中找到的内容来更新此SDT,/docProps/core.xml看起来像这样:

&lt;cp:coreProperties xmlns:cp=&quot;http://schemas.openxmlformats.org/package/2006/metadata/core-properties&quot; xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; &gt;
  &lt;dc:title&gt;我的标题&lt;/dc:title&gt;

有关如何使用docx4j处理该部分的提示,请参阅 https://github.com/plutext/docx4j/blob/VERSION_11_4_8/docx4j-samples-docx4j/src/main/java/org/docx4j/samples/DocProps.java

英文:

Such an SDT will look something like:

&lt;w:sdt&gt;
  &lt;w:sdtPr&gt;
    &lt;w:alias w:val=&quot;Title&quot;/&gt;
    &lt;w:dataBinding w:prefixMappings=&quot;xmlns:ns0=&#39;http://purl.org/dc/elements/1.1/&#39; xmlns:ns1=&#39;http://schemas.openxmlformats.org/package/2006/metadata/core-properties&#39; &quot; w:xpath=&quot;/ns1:coreProperties[1]/ns0:title[1]&quot; w:storeItemID=&quot;{6C3C8BC8-F283-45AE-878A-BAB7291924A1}&quot;/&gt;
    &lt;w:text/&gt;
  &lt;/w:sdtPr&gt;
  &lt;w:sdtContent&gt;
    &lt;w:p&gt;
      &lt;w:r&gt;
        &lt;w:t&gt;My title&lt;/w:t&gt;
      &lt;/w:r&gt;
    &lt;/w:p&gt;
  &lt;/w:sdtContent&gt;
&lt;/w:sdt&gt;

Notice the databinding element; with attribute w:xpath="/ns1:coreProperties[1]/ns0:title[1]"

Word will attempt to update the contents of any SDT which has a databinding element, from its corresponding store (given by w:storeItemID). (That is, updating the text as you describe only works if there is no databinding element)

In this case, it updates this SDT using what it finds in /docProps/core.xml, which looks something like:

&lt;cp:coreProperties xmlns:cp=&quot;http://schemas.openxmlformats.org/package/2006/metadata/core-properties&quot; xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot; &gt;
  &lt;dc:title&gt;My title&lt;/dc:title&gt;

For clues on how to work with that part using docx4j, see https://github.com/plutext/docx4j/blob/VERSION_11_4_8/docx4j-samples-docx4j/src/main/java/org/docx4j/samples/DocProps.java

huangapple
  • 本文由 发表于 2023年6月29日 18:44:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定