Page 1 of 1

How to get the image from a watermark?

PostPosted: Tue Nov 17, 2009 11:00 am
by AnbuChezhian
Hi ,
I am new to this.I hope you will help me.
Is it possible to take images which is saved under media folder through our api ?
If docx contains image watermark,that image file is saved in 'media' folder . How can i fetch that image.Do help me .

Thanks in advance,

Regards,
Anbu Chezhian.S

Re: How to take image files :

PostPosted: Tue Nov 17, 2009 1:29 pm
by jason
In order to answer your question, I added a watermark (choosing picture) to a document in Word 2007, then saved it as XML, then had a look at it in an XML editor (XPontus).

I found that Word added:
Code: Select all
                    <w:sectPr>
                        <w:headerReference w:type="even" r:id="rId6"/>
                        <w:headerReference w:type="default" r:id="rId7"/>
                        <w:footerReference w:type="even" r:id="rId8"/>
                        <w:footerReference w:type="default" r:id="rId9"/>
                        <w:headerReference w:type="first" r:id="rId10"/>
                        <w:footerReference w:type="first" r:id="rId11"/>


with each of the 3 headerReference having a rel pointing to the image "/word/media/image1.jpeg" (a jpeg in this case).

For example:

Code: Select all
    <pkg:part pkg:name="/word/_rels/header1.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
        <pkg:xmlData>
            <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
                <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg"/>
            </Relationships>
        </pkg:xmlData>
    </pkg:part>


to be continued ...

Re: How to take image files :

PostPosted: Tue Nov 17, 2009 1:43 pm
by jason
There are 2 ways you can get the image part.

The first is to get one of the header parts, then ask it for the image part by relationship type. Search this forum for posts on how to get the header part. Then:

Code: Select all
RelationshipsPart rp = headerpart.getRelationshipsPart();
Relationship rel = rp.getRelationshipByType(Namespaces.IMAGE);


Once you have the rel, you can do:

Code: Select all
BinaryPart p = (BinaryPart)rp.getPart(rel);


or, for example

Code: Select all
ImageJpegPart p = (ImageJpegPart)rp.getPart(rel);


The second, only if you already know the name of the part, is to get it directly from the parts collection. For a JPEG, something like:

Code: Select all
ImageJpegPart p = (ImageJpegPart)wordMLPackage.getParts().get(new PartName("/word/media/image1.jpeg" ) )


Look in the samples dir for basics such as how to load the wordMLPackage from a file.

cheers .. Jason

Re: How to get the image from a watermark?

PostPosted: Tue Nov 17, 2009 4:10 pm
by AnbuChezhian
Hi jason ,
Thanks a lot.I can able to proceed till Imagepart. Is it possible to get image as a file after that ?.so that i can do my process on that image.

Re: How to get the image from a watermark?

PostPosted: Wed Nov 18, 2009 12:44 am
by jason
Assuming BinaryPart p and target File f:

Code: Select all
               FileOutputStream fos = new FileOutputStream( f );
               ((BinaryPart)p).writeDataToOutputStream(fos);
               fos.close();

Re: How to get the image from a watermark?

PostPosted: Wed Nov 18, 2009 10:03 am
by AnbuChezhian
Hi Jason ,

Thanks alot it worked so well.
Thank u .