使用java更新word文档中自定义属性的值

发布于 2024-08-19 22:31:14 字数 460 浏览 1 评论 0原文

我无法使用 java 更新 Word 文档中自定义属性的值。

我有一个Word文档,其中包含一个值为“stack”的自定义属性。使用java我想将该值更改为“溢出”。我使用了两种方法。

1)使用Apache POI,

我在org.apache.poi.hpsf.DocumentSummaryInformation中设置org.apache.poi.hpsf.CustomProperties并将其写入POIFSFileSystem。

它确实更新了 word 的自定义属性表中的值。但不会更新文档中相应属性的值。打开文档后,我需要手动刷新文档以更新该值。

2) 逐个字符解析文档,并使用域代码 DC3、DC4 和 NAK 来标识自定义属性的位置。将现有值替换为新值。

现在生成的文档包含新值。但这里的问题是,如果旧值和新值的长度不同,则文档就会损坏。我确保逻辑足够好。

任何帮助将不胜感激。

I am not able to update value of a custom property in a word document using java.

I have a word document which contains a custom property with value 'stack'. Using java I want to change that value to 'overflow'. I used two approaches.

1) Using Apache POI

I set the org.apache.poi.hpsf.CustomProperties in org.apache.poi.hpsf.DocumentSummaryInformation and written it to POIFSFileSystem.

It does updates the value in word's customproperties table. But doesn't update the value for that respective property in the document. After the document is opened, I need to manually refresh the document to get that value updated.

2) Parse the document char by char and use the field codes DC3, DC4 and NAK to identify the location of custom properties. Replace the existing value with the new value.

Now the generated document contains the new values. But the problem here is, if the length of the old value and new value is different the document gets corrupted. I made sure the logic is good enough.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2024-08-26 22:31:14

更新我解决该问题的方法:

使用 Office 2007 (docx) 文档作为模板变得更容易。它只不过是一个 zip 文档。您可以使用 zip 应用程序 (winzip/7zip) 打开它,您可以在其中找到许多 xml 文件。 document.xml包含内容,styles.xml包含格式信息等。

在运行时,我解压缩文档并解析 document.xml,然后使用 dom 并使用动态内容进行更新。自定义属性可在单独的 xml 文件中使用。

对于 2003 用户,他们必须使用 Word 应用程序准备模板并将文档保存为 XML,然后将其作为输入提供。

现在没有使用 apache-poi。

To update the approach I took to solve the issue:

Using Office 2007 (docx) document as a template became easier. It is nothing but a zip document. You can open it using your zip application (winzip/7zip) and you can find many xml files inside it. document.xml contains the content, styles.xml contains formatting information and so on.

At runtime, I unzipped the document and parsed the document.xml, then used dom and updated with dynamic content. Custom properties are available in a separate xml file.

For 2003 users, they have to prepare the template using word application and save the document as XML then provide it as input.

No use of apache-poi now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文