0

I am trying to add an image to the word document I want to create from docx4j..

Here goes my code..

package presaleshelperapplication;

import java.io.ByteArrayOutputStream;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
 import sun.misc.IOUtils;


 public class PreSalesHelperApplication {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
//wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Hello World");
//wordMLPackage.getMainDocumentPart().addParagraphOfText("Text");
 java.io.InputStream is = new           java.io.FileInputStream("/D:/Development/PreSalesData/sample.jpg");
 // commons-io.jar
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 byte[] bytes = baos.toByteArray();


 String filenameHint = null;
 String altText = null;
 int id1 = 0;
 int id2 = 1;
 org.docx4j.wml.P p = newImage( wordMLPackage, bytes,filenameHint, altText,id1, id2,6000  );
 // Now add our p to the document
 wordMLPackage.getMainDocumentPart().addObject(p);
 wordMLPackage.save(new java.io.File("helloworld.docx") );
 is.close();
}



 public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage,
    byte[] bytes,
    String filenameHint, String altText, 
    int id1, int id2, long cx) throws Exception {

    BinaryPartAbstractImage imagePart =             BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);

    Inline inline = imagePart.createImageInline(filenameHint, altText,id1, id2, cx,false);

// Now add the inline in w:p/w:r/w:drawing
    org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
    org.docx4j.wml.P  p = factory.createP();
    org.docx4j.wml.R  run = factory.createR();             
    p.getContent().add(run);       
    org.docx4j.wml.Drawing drawing = factory.createDrawing();               
    run.getContent().add(drawing);               
    drawing.getAnchorOrInline().add(inline);

    return p;
 }
 }

When compiling I am getting the following error...

Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/xmlgraphics/image/loader/ImageContext

My image file is good but getting this error..what could be the prob?

Kara
  • 6,115
  • 16
  • 50
  • 57
user1901079
  • 427
  • 2
  • 7
  • 19

1 Answers1

3

docx4j has dependencies.

One of them is:

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>xmlgraphics-commons</artifactId>
    <version>1.5</version>
</dependency>

You need to add this to your class path.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • I added the jar in the classpath but now I am getting the following exception....openpackagingexceptions:Error checking image format..caused by java.io.EOFException – user1901079 Jul 16 '13 at 06:23
  • You'll have to fix that code you've copy/pasted, so that your variable bytes is actually populated with an image! – JasonPlutext Jul 16 '13 at 07:07
  • I fixed the problem the problem was in the IOUtils import..We need to use commons.io.IOUtils.tobytearray(is) instead of the sun.misc.IOutils – user1901079 Jul 16 '13 at 09:39
  • Holy crap. thanks so much for this! I'm actually working with docx4j with ColdFusion and the error thrown was no help. I threw in the appropriate file for the dependency and it worked right away. Whew! – sugardaddy Sep 17 '18 at 20:55