java - Apache pdfbox .doc to .pdf conversion -


i'm trying convert .doc .pdf, got exception , don't know how fix it.

java.io.ioexception: missing root object specification in trailer @   org.apache.pdfbox.pdfparser.cosparser.parsetrailervaluesdynamically(cosparser.java:2042) 

this exception thrown:

pddocument pdfdocument = pddocument.load(convertdoctopdf(documentinputstream)); 

here conversion method:

private byte[] convertdoctopdf(inputstream documentinputstream) throws exception {     document document = null;     wordextractor = null;     bytearrayoutputstream out = null;     byte[] documentbytearray = null;     try {         document = new document();         poifsfilesystem fs = new poifsfilesystem(documentinputstream);          hwpfdocument doc = new hwpfdocument(fs);         = new wordextractor(doc);         out = new bytearrayoutputstream();         pdfwriter writer = pdfwriter.getinstance(document, out);          range range = doc.getrange();         document.open();         writer.setpageempty(true);         document.newpage();         writer.setpageempty(true);          string[] paragraphs = we.getparagraphtext();         (int = 0; < paragraphs.length; i++) {             org.apache.poi.hwpf.usermodel.paragraph pr = range.getparagraph(i);             paragraphs[i] = paragraphs[i].replaceall("\\cm?\r?\n", "");             document.add(new paragraph(paragraphs[i]));         }         documentbytearray = out.tobytearray();     } catch (exception ex) {         ex.printstacktrace(system.out);         throw new exception(state.failed_conversion.name());     } {         document.close();         try {             we.close();             out.close();         } catch (ioexception e) {             e.printstacktrace();         }     }     return documentbytearray; } 

you use itext classes , do

documentbytearray = out.tobytearray(); 

before finish document

document.close(); 

thus, documentbytearray contains incomplete pdf pdfbox complains about.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -