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
<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.
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 :(