java - Apache POI 无法保存 (HWPFDocument.write) 大型 Word 文档文件

标签 java ms-word apache-poi doc

我想从 .doc 文件中删除单词元数据。我的 .docx 文件可以与 XWPFDocument 配合使用,但以下用于删除元数据的代码对于大型 (> 1MB) 文件会失败。例如,使用带有图像的 6MB .doc 文件,它会输出一个 4.5MB 文件,其中删除了一些图像。

public static InputStream removeMetaData(InputStream inputStream) throws IOException {
    POIFSFileSystem fss = new POIFSFileSystem(inputStream);
    HWPFDocument doc = new HWPFDocument(fss);

    // **it even fails on large files if you remove from here to 'until' below**
    SummaryInformation si = doc.getSummaryInformation();
    si.removeAuthor();
    si.removeComments();
    si.removeLastAuthor();
    si.removeKeywords();
    si.removeSubject();
    si.removeTitle();

    doc.getDocumentSummaryInformation().removeCategory();
    doc.getDocumentSummaryInformation().removeCompany();
    doc.getDocumentSummaryInformation().removeManager();
    try {
        doc.getDocumentSummaryInformation().removeCustomProperties();
    } catch (Exception e) {
        // can not remove above
    }
    // until

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    doc.write(os);
    os.flush();
    os.close();
    return new ByteArrayInputStream(os.toByteArray());
}

相关帖子:

最佳答案

您使用的是哪个版本的 Apache POI?

这似乎是Bug 46220 - Regression: Some embedded images being lost .

请升级到latest release of POI (3.8)然后再试一次。

希望有帮助。

关于java - Apache POI 无法保存 (HWPFDocument.write) 大型 Word 文档文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13233164/

相关文章:

java - 理解这个 Java 程序的执行流程

java - 使用 Apache poi 在 Stacked bar 上方显示 SUM 值

java - HSSFWorkbook java.lang.IllegalArgumentException : No more than 3 rules may be specified

java - 确保所有任务完成的同步对象

java - 在运行时使用一些字符串调用方法

Java String[] args 缺少字符

python - 杀死像任务管理器

vba - 用于一对多拆分 Word 文档的宏

ms-word - 如何将代码插入Word并另存为MD - writage插件删除代码格式

java - 从Excel中读取表格数据并在Java中显示唯一的列组合数