Created attachment 34995 [details] The attached document gets corrupted after adding custom attributes Following code is used to add custom attributes to the attached document private String updateWordDocContent(String tempOutputFile) { File inputFile = null; File outputFile = null; FileInputStream fileIStream = null; FileOutputStream fileOStream = null; HWPFDocument wordDoc = null; try { System.out.println("updateWordDocContent() "); // APACHE POI inputFile = new File(tempOutputFile); fileIStream = new FileInputStream(inputFile); wordDoc = new HWPFDocument(fileIStream); DocumentSummaryInformation docSummary = wordDoc.getDocumentSummaryInformation(); System.out.println(docSummary.toString()); CustomProperties cp = docSummary.getCustomProperties(); if (cp == null) cp = new CustomProperties(); cp.put("Product", ""); cp.put("Author", "Sean Smith"); cp.put("OS", "Linux"); cp.put("Comments", "Code for POI Word With Doc"); cp.put("extension", "Super OTX"); docSummary.setCustomProperties(cp); fileIStream.close(); outputFile = new File(tempOutputFile+"-customPro"+System.nanoTime()+".doc"); fileOStream = new FileOutputStream(outputFile); wordDoc.write(fileOStream); fileOStream.flush(); fileOStream.close(); return tempOutputFile; } catch (Exception e) { DfLogger.error(this, "Error: " + e.getMessage(), null, e); e.printStackTrace(); } finally { if (fileIStream != null) { try { fileIStream.close(); fileIStream = null; } catch (Exception ex) { } } if (fileOStream != null) { try { fileOStream.flush(); fileOStream.close(); fileOStream = null; } catch (Exception ex) { // I G N O R E // } } } return null; }
I don't even need to add the custom property, a simple reading of the file and writing out again into another file makes the resulting file corrupted when opened with Microsoft Word. Also doing a HWPFTestDataSamples.writeOutAndReadBack(wordDoc) also fails with some error message, so it seems something is already corrupted either when loading or when writing that document.