wordpress hit counter
Re: Insert a picture into an openxml document - Java - Development Tools - OpenXML Developer

Re: Insert a picture into an openxml document

Development Tools

Discussions about working with Open XML using a wide range of development tools

Insert a picture into an openxml document

  • rated by 0 users
  • This post has 3 Replies |
  • 3 Followers
  • hi,
    i am new user of openxml format, and i would like to insert a picture using java code into an openxml document.i used this new and incomplete framework to insert paragraph in the documentis.but there are not yet methods that help to insert images.is there a link or a book that can help me to do this?
    thanks
  • Hi,

    Find the following steps to insert a picture in to docx file using java:

    Step: 1

    Unzip the docx file,using the packages

    import java.util.zip.ZipEntry;

    import java.util.zip.ZipOutputStream;

    http://www.exampledepot.com/egs/java.util.zip/CreateZip.html

    the above link provides the information about the zipping and unzipping the packages.

     

    Step: 2

    The images are basically stored in a folder external to the document, hence create a folder ,and name it ‘media’ ,in the unzipped folder structure

                         String[] dirStructure={

                                            workAreaPath,

                                            workAreaPath+pathBreak+"_rels",

                                            workAreaPath+pathBreak+"word",

                                            workAreaPath+pathBreak+"word"+pathBreak+"_rels",

                                            workAreaPath+pathBreak+"word"+pathBreak+"media"

                                            };

                            for (int row =0; row < dirStructure.length; row++)

                            {

                                File newDir = new File(dirStructure[row]);

                                newDir.mkdir(); 

                            }

               

    Here, dirStructure is an array variable which holds the docx file directory structure.         

    ‘workAreaPath’, is the location where you unzip the word document.

    ‘pathBreak’ is the path separator(‘/’).

     

    Step: 3

     

    Place the image file in the media folder

     

    /* copying mediafiles from source folder to package structure media folder */

     

                            String dst=workAreaPath+pathBreak+"word"+pathBreak+"_rels";

                            File srcDir=null;

                            srcDir= new File(src);

                            File dstDir=null;

                            dstDir= new File(dst);

                            copyDirectory(srcDir,dstDir);

     

    src is the  image folder location where we will pick the picture.

    dst is the folder location where we place the images.

     

    Step :4

     

    You need to mention the content type of the image in

     

    [Content_Types].xml

     

    Added tag :

     

    <Default Extension="jpg" ContentType="image/jpg"/>

     

    document.xml.rels

     

    Added tag :

     

    <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpg"/>

     

    header.xml.rels

     

    create header.xml.rels with the below tags

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

    <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image2.jpg"/></Relationships>

     

    Step :5

     

    Zip the modified folder structure and rename to docx file.

    Finally you can find the image on the docx file.

    srikanth
  • thanks srikanth for your immediate response.i'am applying your solution.
    thanks again
  • You described it very nicely. But I can not find the part about adding the image link to the actual document.

    Can you help me here?

    I know it is been 5 years :(

Page 1 of 1 (4 items)