我想從.doc文件中刪除單詞元數據。我的.docx
文件適用於XWPFDocument
,但以下用於刪除大型(> 1MB)文件的元數據代碼失敗。例如,將6MB .doc文件與圖像一起使用時,會輸出一個4.5MB文件,其中某些圖像將被刪除。Apache POI無法保存(HWPFDocument.write)大文檔doc文件
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());
}
相關文章:
- How to save the Word Document using POI API?
- https://stackoverflow.com/questions/9758955/saving-poi-document-correctly
我不明白你的問題。是問題,你有一些例外,還是你有太大的輸出文件=>要刪除的東西仍然剩下? –
元數據被正確刪除,但給了一個帶表格,頁腳,標題和圖像的6mb文件,poi會輸出一個〜4.5mb的文件,刪除元數據,但也會刪除一些隨機圖像。 – Mohsen
如果隨機圖像被刪除(每個調用刪除了其他東西),那麼它必然是線程問題,或者是例如錯誤地發佈了內存錯誤。線程問題可以被檢查,如果你在一個線程中運行它,那麼它可以正常工作。制動點可發現內存異常。我建議以調試模式運行代碼,並設置一個異常brakepoints(catch和uncatched以及)來捕獲錯誤和RuntimeExceptions。也許你會發現有趣的東西。 –