繁体   English   中英

如何读取旧字 doc 文件元数据

[英]How to read old word doc file metadata

假设我想将一个带有doc扩展名的 word 文件连同元数据一起导入到我的 HTML 文档中,并相应地在一个div显示它。 因此doc文件中的所有现有内容,例如各种格式的文本(粗体、斜体、不同大小、字母间距、行高、上划线、下划线……)、图像(它们的位置和大小)、图形、图表( JSP 将生成必要的图形以提供类似外观的图形或图表。它只需要数据)、列表等。

那么有没有办法做到这一点? 是否有任何标准化的 Word API 可以为我们提供这些数据? 或者任何可以做到这一点的JSP库? 如果没有,那么我需要知道什么并做些什么才能得到这个?

查看 Apache POI 项目: http : //poi.apache.org/text-extraction.html以及 Apache Tika: http : //tika.apache.org/

5年后,答案是:

注意:此代码仅适用于旧词“doc”文件(不是 docx),Apache POI 也可以处理 docx,但您必须使用其他 API。

使用Apache POI ,Maven 依赖项:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.17</version>
</dependency>

这是代码:

  ...
  import org.apache.poi.poifs.filesystem.DirectoryEntry;
  import org.apache.poi.poifs.filesystem.DocumentEntry;
  import org.apache.poi.poifs.filesystem.DocumentInputStream;
  import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  public static void main(final String[] args) throws FileNotFoundException, IOException, NoPropertySetStreamException,
                  MarkUnsupportedException, UnexpectedPropertySetTypeException {
      try (final FileInputStream fs = new FileInputStream("src/test/word_template.doc");
        final POIFSFileSystem poifs = new POIFSFileSystem(fs)) {
        final DirectoryEntry dir = poifs.getRoot();
        final DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
        try (final DocumentInputStream dis = new DocumentInputStream(siEntry)) {
          final PropertySet ps = new PropertySet(dis);
          final SummaryInformation si = new SummaryInformation(ps);
          // Read word doc (not docx) metadata.
          System.out.println(si.getLastAuthor());
          System.out.println(si.getAuthor());
          System.out.println(si.getKeywords());
          System.out.println(si.getSubject());
          // ...
        }
      }
    }

要阅读文本内容,您将需要额外的依赖项:

<dependency>
  <!-- Required for HWPFDocument -->
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-scratchpad</artifactId>
  <version>3.17</version>
</dependency>

代码:

try (final HWPFDocument doc = new HWPFDocument(fs)) {
  return doc.getText().toString();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM