java - Unable to Add Image to Document Using DocX4J -


i'm trying add image document (.docx) using docx4j library code below. image exists in local machine , taught doesn't support png , have renamed image jpg , still throws error

string usersignaturefile = "c:\\esignature\\sign.jpg";              // read signature image bytes              inputstream inputstream = new java.io.fileinputstream(usersignaturefile);             long filelength = usersignaturefile.length();                  byte[] bytes = new byte[(int)filelength];              int offset = 0;             int numread = 0;              while(offset < bytes.length                    && (numread = inputstream.read(bytes, offset, bytes.length-offset)) >= 0) {                 offset += numread;             }              inputstream.close();              string filenamehint = null;             string alttext = null;              int id1 = 0;             int id2 = 1;              // create inline image              binarypartabstractimage imagepart = binarypartabstractimage.createimagepart(wordpackage, bytes);             inline inline = imagepart.createimageinline( filenamehint, alttext, id1, id2);              // create drawing , add run               drawing imagedrawing = factory.createdrawing();                imagedrawing.getanchororinline().add(inline);             // add text run             run.getcontent().add(imagedrawing);              // add run paragraph             ((p) jaxbnode).getcontent().add(run); 

and below error message

    exception in thread "main" org.docx4j.openpackaging.exceptions.docx4jexception: error checking image format         @ org.docx4j.openpackaging.parts.wordprocessingml.binarypartabstractimage.ensureformatissupported(binarypartabstractimage.java:429)         @ org.docx4j.openpackaging.parts.wordprocessingml.binarypartabstractimage.ensureformatissupported(binarypartabstractimage.java:331)         @ org.docx4j.openpackaging.parts.wordprocessingml.binarypartabstractimage.createimagepart(binarypartabstractimage.java:225)         @ org.docx4j.openpackaging.parts.wordprocessingml.binarypartabstractimage.createimagepart(binarypartabstractimage.java:144)  caused by: java.io.ioexception: cannot run program "imconvert": createprocess error=2, system cannot find file specified     @ java.lang.processbuilder.start(unknown source)     @ java.lang.runtime.exec(unknown source)     @ java.lang.runtime.exec(unknown source)     @ java.lang.runtime.exec(unknown source)     @ org.docx4j.openpackaging.parts.wordprocessingml.binarypartabstractimage.converttopng(binarypartabstractimage.java:905)     @ org.docx4j.openpackaging.parts.wordprocessingml.binarypartabstractimage.ensureformatissupported(binarypartabstractimage.java:413)     ... 6 more caused by: java.io.ioexception: createprocess error=2, system cannot find file specified     @ java.lang.processimpl.create(native method)     @ java.lang.processimpl.<init>(unknown source)     @ java.lang.processimpl.start(unknown source)     ... 12 more 

actually mistake used input stream , directly passed file path string (without file ) , after correcting below, worked.

correct

file file = new file(usersignaturefile);              // read signature image bytes              inputstream inputstream = new java.io.fileinputstream(file); 

wrong

        inputstream inputstream = new java.io.fileinputstream(usersignaturefile); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -