Page 1 of 1

Copying Table Containing Image

PostPosted: Fri Dec 28, 2012 5:28 pm
by Zaeem
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?

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

Re: Copying Table Containing Image

PostPosted: Fri Dec 28, 2012 8:44 pm
by Zaeem
Anyways, I found an alternate way to do this thing as follows:
1. Get all Tc's (list) from the table to be inserted using some helper function (I used getAllElementFromObject() function, with object being the table and Tc being the class)
2. Tc screenShotCell = (Tc) tableCellsList.get(1);
3. Just add the inline paragraph image using screenShotCell.getContent().add(p);

But, jason, do reply if you now the work around for previous problem.
Thanks!

Re: Copying Table Containing Image

PostPosted: Fri Dec 28, 2012 9:42 pm
by jason
Zaeem wrote: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?


Does your newImage function take care of the relId properly? ie the relId of the added image part will need to be included in the XML representing the image in the document.

Zaeem wrote:"java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to org.docx4j.wml.Tc".


A Tc is one of the elements which is unmarshalled wrapped in a JAXBElement. You can either test for this, or just use XmlUtils.unwrap on it.