0

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
JavaGeek
  • 1,535
  • 9
  • 39
  • 63
  • 1
    If the PNG format is not supported, then *renaming* won't help – changing the filename doesn't change the data within the file! – lenz Jun 14 '15 at 21:13
  • Even i have created new jpg and still it doesn't support and gives error as org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:429) – JavaGeek Jun 14 '15 at 21:19
  • Apparently the file cannot be found. Are you sure the path is correct? – lenz Jun 14 '15 at 21:40

1 Answers1

0

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);
JavaGeek
  • 1,535
  • 9
  • 39
  • 63