Unable to Add Image to Document Using DocX4J

1.5k views Asked by At

I'm trying to add Image to Document (.docx) using Docx4j library with code like below. The image already exists in local machine and initially i taught it doesn't support png and then i have renamed the image to jpg and it still throws error

String userSignatureFile = "C:\\esignature\\sign.jpg";

            // read the signature image into 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 and add to Run
              Drawing imageDrawing = factory.createDrawing();
               imageDrawing.getAnchorOrInline().add(inline);
            // add Text to Run
            run.getContent().add(imageDrawing);

            // add Run to Paragraph
            ((P) jaxbNode).getContent().add(run);

And below is the error message

    Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Error checking image format
        at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:429)
        at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:331)
        at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:225)
        at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:144)

Caused by: java.io.IOException: Cannot run program "imconvert": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.convertToPNG(BinaryPartAbstractImage.java:905)
    at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:413)
    ... 6 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 12 more
1

There are 1 answers

0
JavaGeek On BEST ANSWER

Actually its my mistake where i used input stream and directly passed the file path string (without File ) and after correcting as below, it worked.

Correct

File file = new File(userSignatureFile);

            // read the signature image into Bytes

            InputStream inputStream = new java.io.FileInputStream(file);

Wrong

        InputStream inputStream = new java.io.FileInputStream(userSignatureFile);