无法使用DocX4J将图像添加到文档

时间:2015-06-14 20:59:58

标签: java docx4j

我尝试使用Docx4j库将Image添加到Document(.docx),代码如下所示。图像已经存在于本地机器中,最初我教它不支持png然后我将图像重命名为jpg并且仍然会抛出错误

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);

以下是错误消息

    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 个答案:

答案 0 :(得分:0)

实际上是我的错误,我使用输入流并直接传递文件路径字符串(没有文件),并在如下更正后,它工作。

正确

File file = new File(userSignatureFile);

            // read the signature image into Bytes

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

错误

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