Page 1 of 1

Adding a image to a Paragraph

PostPosted: Sat Jan 28, 2012 4:04 am
by sureshbabubv
Dear All,

I am trying to add a picture (org.docx4j.wml.Pict) into a Paragraph by using the following code.

It runs perfectly(no runtime error, creating the docx file) but when I try to open that file using MS-WORD is is showing error in file.

I tried to open it using wordpad it is opening fine and the inserted image is appearing.

Why this generated file is not opening with MS-Word

Please advice me, where I am doing the mistake.

Thanks & Regards,
B.V.Suresh Babu.

===================================================================================================


Code: Select all
try
    {
      
       startParg();startRun();
       int i=0;
      
       
       File file = new File(System.getProperty("user.dir") +"/"+PropertyUtility.getKeyValue(APP_HEADER_LOGO_PATH) );
         
         java.io.InputStream is = new java.io.FileInputStream(file );
        long length = file.length();   
        
        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;
              
        Pict objPict = wmlFactory.createPict();;
        
        objPict.getAnyAndAny().add(newImage( bean.getDoc(), bytes,
                 filenameHint, altText,
                id1, id2 ));
        
      
        bean.getCurrentParg().getContent().add(objPict);;
      
       
            endRun();endParg();
    }
    catch(Exception e)    {
       e.printStackTrace();
    }

Re: Adding a image to a Paragraph

PostPosted: Tue Jan 31, 2012 6:53 pm
by sureshbabubv
I resolved this issue by the using the following steps

1) I added an image to the the Image-part for Inline

2) give the relationship id of that one to the pict that we have to insert in the paragraph.


Thanks & Regards,
B.V.Suresh Babu.

Re: Adding a image to a Paragraph

PostPosted: Fri Jun 29, 2012 9:39 pm
by keerthi
Hi Suresh Babu,
Can you please tell me what did you do in the newImage method in your code ? If possible please share the same.
I am trying to insert a picture object, but i dont want to generate and add v:formulas object.

Thanks in advance.
Keerthi G

Re: Adding a image to a Paragraph

PostPosted: Wed Jul 04, 2012 9:31 am
by jason
Any reason you want a VML picture as opposed to the newer DrawingML approach (for which there is a sample, ImageAdd)?