Reputation: 15
Hey guys note this appears to be a similar problem to a previous post - but the solution posted there does not resolve my error.
My requirements were to,
1) load multiple word documents and 2) merger the word documents into a single word document.
Guys some effort I was able to merge the files using the Apache POI API. However once I attempt to the open the merged file, Microsoft Word generates the following error,
"Word found unreadable content in final.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes."
Guys note Microsoft Word is however able to fully recover the file, after I click 'YES'; the resultant files having been correctly merged. However I would like to eliminate the file corrupt error warning, if possible.
My code listing for the merger class,
package rx.reportgenerator.gh;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
/**
*
* @author kwadwooteng-amoko
*/
public class WordMerge {
private final OutputStream merger;
private final List<InputStream> docs;
private XWPFDocument first;
public WordMerge(OutputStream result) {
this.merger = merger;
docs = new ArrayList<>();
}
public void add(InputStream document) throws Exception{
docs.add(document);
OPCPackage src = OPCPackage.open(document);
XWPFDocument xDocument = new XWPFDocument(src);
XWPFParagraph paragraph = xDocument.createParagraph();
paragraph.setPageBreak(true);
if(docs.size() == 1){
first = xDocument;
} else {
CTBody xBody = xDocument.getDocument().getBody();
first.getDocument().addNewBody().set(xBody);
}
}
public void doMerge() throws Exception{
first.write(merger);
}
public void close() throws Exception{
merger.flush();
merger.close();
for (InputStream input : inputs) {
input.close();
}
}
}
My library dependancies to the best of my knowledge, are all upto date:
No idea guys why the code is generating an error. Although the merger completed accurately. Can you help me guys
Upvotes: 0
Views: 1408
Reputation: 3
Not sure if this problem has already been solved. However, as I just faced the same issue with this error I would like to share my solution for others:
When generating empty table cells with apache poi in a word file, the same error occurs. For some reason, word does not like those empty cells. After I added .addParagraph() for all emtpy cells in my table, the error disappeared. Source: https://bz.apache.org/bugzilla/show_bug.cgi?id=65292
Upvotes: 0