Bug 61098 - Certain documents containing forms and XML data get corrupted adding custom properties
Summary: Certain documents containing forms and XML data get corrupted adding custom ...
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: HWPF (show other bugs)
Version: 3.16-FINAL
Hardware: PC All
: P2 major with 1 vote (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-15 15:51 UTC by Ravi Nerella
Modified: 2017-09-26 12:15 UTC (History)
0 users



Attachments
The attached document gets corrupted after adding custom attributes (28.50 KB, application/msword)
2017-05-15 15:51 UTC, Ravi Nerella
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ravi Nerella 2017-05-15 15:51:49 UTC
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;
	}
Comment 1 Dominik Stadler 2017-09-26 12:15:41 UTC
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.