我想从 .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/