java - 通过 Apache POI 将自定义(扩展)属性添加到 docx 和段落

标签 java apache-poi xwpf

我想使用 Apache POI 将文件从 *.fidus (Fidus Writer) 平台转换为 *.docx 格式,反之亦然。

在 *.fidus 文件中有一些属性,我需要将它们作为扩展或自定义属性存储在 *.docx 文件中,然后当我想将其转换回 *.fidus 时我可以检索它们。

因此我想知道如何使用 POI 的类 CustomProperties 或类似的东西将一些属性添加到 docx 文件。是否可以使用 POI 将自定义属性(扩展属性)添加到 docx 文件中的段落?

提前致谢。

最佳答案

由于*.docx 文档是基于XML 的,我们必须使用POIXMLProperties.CustomProperties,参见http://poi.apache.org/apidocs/org/apache/poi/POIXMLProperties.CustomProperties.html .

例子:

import java.io.*;
import org.apache.poi.*;
import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;

import java.util.GregorianCalendar;

public class DocumentProperties {

 public static void main(String[] args) throws IOException {

  XWPFDocument document = new XWPFDocument(new FileInputStream("This is a Test.docx"));

  POIXMLProperties properties = document.getProperties();
  //http://poi.apache.org/apidocs/org/apache/poi/POIXMLProperties.html

  //prints the core property Creator:
  System.out.println(properties.getCoreProperties().getCreator());

  //prints the extendend property Application:
  System.out.println(properties.getExtendedProperties().getApplication());

  //sets a custom property
  POIXMLProperties.CustomProperties customproperties = properties.getCustomProperties();
  if (!customproperties.contains("Test")) {
   customproperties.addProperty("Test", 123);
  }
  CTProperty ctproperty = customproperties.getProperty("Test");
  System.out.println(ctproperty);
  System.out.println(ctproperty.getI4());

  //the above customproperties.addProperty() can only set boolean, double, integer or string properties
  //the CTProperty contains more possibitities
  if (!customproperties.contains("Test Date")) {
   customproperties.addProperty("Test Date", 0);
   ctproperty = customproperties.getProperty("Test Date");
   ctproperty.unsetI4();
   ctproperty.setFiletime(new GregorianCalendar(2016,1,13));
  }
  ctproperty = customproperties.getProperty("Test Date");
  System.out.println(ctproperty);
  System.out.println(ctproperty.getFiletime());


  FileOutputStream out = new FileOutputStream(new File("This is a Test.docx"));
  document.write(out);
 }
}

POIXMLProperties.CustomProperties.addProperty() 只能设置 boolean 、 double 、整数或字符串属性,但底层的 CTProperty 包含更多可能性。

对于 CTProperty,请参阅 http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/officeDocument/x2006/customProperties/CTProperty.java#CTProperty .

关于java - 通过 Apache POI 将自定义(扩展)属性添加到 docx 和段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35359600/

相关文章:

java - java中的LinkedHashMap

java - 更新 Android Studio 后链接引用资源和重复值失败

java - ScatterChart 使用 LineMarker 作为 ScatterStyle 而不是仅使用 Marker

java - 如何使用 apache-poi 水平合并单元格

java - XWPF POI 如何在没有自动换行的情况下设置段落中的文本

java - 刷新XWPF文档更改

Java 安全异常 : signer information does not match

java - 如何在 ubuntu 中为 VS 代码配置和下载 Java JDK

java.lang.ClassNotFoundException : org. apache.poi.xwpf.usermodel.IRunBody

java - 如何使用 apache poi 3.1 获取公式单元格值(数据)