Search code examples
xmlimagems-worddocx

Changing XML of docx to load images from web


Basically, my docx size is very big and it has many images and I wanted to reduce the size of it, I tried everything, compressed the images and etc, so from 25MB I got it to 13MB. But I wanted to lower it more so I was playing around and I changed the name of docx extension to zip and extracted it and saw the content of the files, saw the images, so I uploaded the images on my web-hosting and in the xml file, there was a file that was calling the images.

 <?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
 <Relationship Target="media/image99.jpeg" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId117"/>
 <Relationship Target="media/image273.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId299"/>
 <Relationship Target="media/image13.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId21"/>

So I changed the "Target" to my site, for example:

 <Relationship Target="http://example.com/imagesfordoc/image13.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId21"/>

So I did change all of them with replace function and they're perfectly like the one in example (obviously with different image names). I deleted the media folder as it was not needed, then it as it was again then changed the extension from zip to docx, ran it and it gives me error that the file is corrupted and then I click okay then it asks me to recover, and if I click yes, it opens the word document as it was but without images (Just text with its styling set before)

The images are uploaded and opening perfectly on normal browser but when I open the docx. there's an error!

Any idea how I can achieve this?


Solution

  • Found the answer from here: http://blogs.msdn.com/b/dmahugh/archive/2006/12/10/images-in-open-xml-documents.aspx

    Needed to add a param:

     TargetMode="External"