Page 1 of 1

Word Image Issues

PostPosted: Wed Jun 26, 2013 2:59 am
by huskygrad
Hi,

I am new to this forum and have been using docx4j for manipulating word documents. Recently, I have been have issues inserting image in a table cell (Tc). Here my code.
Code: Select all
image = BinaryPartAbstractImage.createImagePart(WordprocessingMLPackage.createPackage(), imageByteArray);
CTSdtCell cell = (CTSdtCell)element.getValue();
JAXBElement cellElement =  (JAXBElement)cell.getSdtContent().getContent().get(0);
// Get the cell from the CTSdtCell
Tc tableCell = (Tc)cellElement.getValue();

Inline inline = image.createImageInline("", "", 1,2,false);
P imageP = factory.createP();
R imageRun = factory.createR();
imageP.getContent().add(imageRun);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
imageRun.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
tableCell.getContent().add(0, imageP);


When I save the document, I don't have any runtime errors. When I open the document, I cannot find the image, but the word shows "The Image currently cannot be displayed". I have trying to fix this issues, but was unable to find any solutions.

Re: Word Image Issues

PostPosted: Wed Jun 26, 2013 8:58 am
by jason
createImagePart(WordprocessingMLPackage.createPackage() .. is adding your new image part to a new WordprocessingMLPackage, different to the one containing your CTSdtCell cell?

To debug, generally you should use XmlUtils.marshaltoString on P imageP, or unzip your resulting docx; this might reveal additional problems. Please post the resulting XML in any follow up post.

Re: Word Image Issues

PostPosted: Thu Jun 27, 2013 1:26 am
by huskygrad
Hi Jason,

Thanks for the help. Your comment
createImagePart(WordprocessingMLPackage.createPackage() .. is adding your new image part to a new WordprocessingMLPackage, different to the one containing your CTSdtCell cell?
helped me. Instead of creating a new package, I have passed in the package I have created when reading the document.

Code: Select all
File f = new File("C:\\report.docx");
InputStream in = new FileInputStream(f);
doc = WordprocessingMLPackage.load(in);

image = BinaryPartAbstractImage.createImagePart(doc,imageByteArray);