Copying Table Containing Image
Posted: Fri Dec 28, 2012 5:28 pm
I am trying to copy a table, having one cell containing an image, from one DOCX to another. But after copying, the resultant DOCX, shows: "The image cannot currently be displayed" in a box where the image should be present. What could I possibly be missing?
One more thing, that when I tried to access a particular cell of a table using the code given below, I get an exception stating that "java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to org.docx4j.wml.Tc". What is the problem here?
One more thing, that when I tried to access a particular cell of a table using the code given below, I get an exception stating that "java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to org.docx4j.wml.Tc". What is the problem here?
- Code: Select all
static Tbl addImageInTable (WordprocessingMLPackage templateDocument, Tbl table, String imageName) throws Exception {
File file = new File("C:\\ImgTest\\test.BMP");
//File file = new File("C:\\ImgTest\\" + imageName);
java.io.InputStream is = new java.io.FileInputStream(file );
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
System.out.println("Could not completely read file "+file.getName());
}
is.close();
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
org.docx4j.wml.P p = newImage(templateDocument, bytes,
filenameHint, altText,
id1, id2 );
// Now add our p to the document
//wordMLPackage.getMainDocumentPart().addObject(p);
//table.getContent().add(p);
List rows = table.getEGContentRowContent();
Tr row = (Tr) rows.get(0);
List cells = row.getEGContentCellContent();
Tc tc = (Tc) cells.get(0);
tc.getEGBlockLevelElts().add(p);
return table;
}