Open Microsoft Word in Java
I am trying to open a MS Word 2003 document in java, search for the specified string and replace it with a new String. For this I am using APACHE POI. My code looks like this:
public void searchAndReplace(String inputFilename, String outputFilename,
HashMap<String, String> replacements) {
File outputFile = null;
File inputFile = null;
FileInputStream fileIStream = null;
FileOutputStream fileOStream = null;
BufferedInputStream bufIStream = null;
BufferedOutputStream bufOStream = null;
POIFSFileSystem fileSystem = null;
HWPFDocument document = null;
Range docRange = null;
Paragraph paragraph = null;
CharacterRun charRun = null;
Set<String> keySet = null;
Iterator<String> keySetIterator = null;
int numParagraphs = 0;
int numCharRuns = 0;
String text = null;
String key = null;
String value = null;
try {
// Create an instance of the POIFSFileSystem class and
// attach it to the Word document using an InputStream.
inputFile = new File(inputFilename);
fileIStream = new FileInputStream(inputFile);
bufIStream = new BufferedInputStream(fileIStream);
fileSystem = new POIFSFileSystem(bufIStream);
document = new HWPFDocument(fileSystem);
docRange = document.getRange();
numParagraphs = docRange.numParagraphs();
keySet = replacements.keySet();
for (int i = 0; i < numParagraphs; i++) {
paragraph = docRange.getParagraph(i);
text = paragraph.text();
numCharRuns = paragraph.numCharacterRuns();
for (int j = 0; j < numCharRuns; j++) {
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
System.out.println("Character Run text: " + text);
keySetIterator = keySet.iterator();
while (keySetIterator.hasNext()) {
key = keySetIterator.next();
if (text.contains(key)) {
value = replacements.get(key);
charRun.replaceText(key, value);
docRange = document.getRange();
paragraph = docRange.getParagraph(i);
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
}
}
}
}
bufIStream.close();
bufIStream = null;
outputFile = new File(outputFilename);
fileOStream = new FileOutputStream(outputFile);
bufOStream = new BufferedOutputStream(fileOStream);
document.write(bufOStream);
} catch (Exception ex) {
System.out.println("Caught an: " + ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows.............");
ex.printStackTrace(System.out);
}
}
I am calling this function with the following arguments:
HashMap<String, String> replacements = new HashMap<String, String>();
replacements.put("AAA", "BBB");
searchAndReplace("C:/Test.doc", "C:/Test1.doc", replacements);
When the Test.doc file contains a simple line: " AAA EEE " it works successfully, but when I use a complex file it will read the content successfully and generate Test1. doc, but when I try to open it it gives me the following error:
Word cannot read this document. It could be damaged. Try one or more of the following: * Open and repair the file. * Open the file with Text Recovery Converter. (C: \ Test1.doc)
Please tell me what to do because I am new to POI and I have not found a good tutorial for it.
a source to share
First of all, you must close the document.
Also, what I suggest to do is save the original Word document as a Word XML document and then manually change the extension from .XML to .doc. Then look at the XML of the actual document you are working with and trace the content to make sure you are not arbitrarily editing the hex values (AAA and EEE can be hex values in other fields).
Without seeing the actual Word document, it's hard to tell what's going on.
There is not much documentation about POIs, especially for a Word document.
a source to share
I don't know if it's okay for me to answer, but in order to share my knowledge, I will answer myself.
After browsing the internet, the final solution I found is this: A library called docx4j is very good for working with MS docx file, although its documentation is not enough so far and its forum is still in the first steps, but overall it helps me do what i need.
Thank you 4 everyone who helps me ..
a source to share