Page 1 of 1

Replacing images in doc

PostPosted: Sat Dec 18, 2010 12:05 am
by kjetilhp
I've read several posts here on image manipulation and gone trough the samples of adding images, and are still not sure how to go about replacing images..

so a couple of questions,

should I use the Picture Content Control binding? not sure how that would work (found a sample here http://www.codeproject.com/KB/office/Wo ... mages.aspx but it's in C# or something...)

or

should I have an placeholder image in the template, then in code, remove that part, add new image and update the relationship. If so, how do you do that? (removing and updating I mean)

or a better approach? :)

thx,
K

Re: Replacing images in doc

PostPosted: Mon Dec 20, 2010 4:27 pm
by jason
If you have an existing image of identical type (eg jpg) in the document already which you want to replace, you can find its Part, and replace the binary content in the part using any of the methods:

Code: Select all
   public void setBinaryData(InputStream binaryData);
   public void setBinaryData(byte[] bytes);
   public void setBinaryData(ByteBuffer bb);


The main document part specifies dimensions etc though, so unless these are the same, you'll need to adjust there.

You may find it easier to just start from scratch (ie delete the image rel from the rels part, and remove the XML in the main document part which references it, then add your new image as per the AddImage sample). If there was a ReplaceImage method, that is what it would do (though it would check for edge case where the rel was referenced a second time from somewhere else)

To delete the image rel from the rels part, use RelationshipsPart methods:

Code: Select all
public void removePart(PartName partName)

or public void removeRelationship(Relationship rel)