Replacing an existing image in a doc without losing formatting and placement

Mar 3, 2012 at 3:14 PM

Hi,

I have a document that I included an inserted picture in.  The placement is very specific... all the way on the right top with the text flow set up around it.  

What I need to do is replace that image with another image via code so that the positioning and settings are not lost OR replicate all the settings by inserting the new picture in the document, 

I've not been successful with either... i can remove the original image and insert the new one close by, but it never comes out quite right, it messes with the text going around it or ends up on the left.  I've tried to use the alignment, etc, but nothing seems to get it quite right.

Any ideas on how this could be done?

 

PS. This code has worked great for everything else in this project!

Coordinator
Mar 3, 2012 at 5:39 PM
Hi Bchmura,

so you have already tried replacing the image using DocX but something is going wrong with your very specific document layout?

You could try just modifying the actual image. This would not effect the document's structure at all.
I wrote a cool example, some time ago on my blog, about doing just this.

Instead of

  1. // Draw the string "Hello World".
  2. g.DrawString
  3. (
  4. str,
  5. new Font("Tahoma", 20),
  6. Brushes.Blue,
  7. new PointF(0, 0)
  8. );

Try experimenting with g.DrawImage()

I am not in a position at the moment (I'm not on my development machine) to give this a try.
Please let me know if this solves your problem.

Kind regards and happy coding,
Cathal
Mar 3, 2012 at 6:02 PM
Edited Mar 3, 2012 at 6:17 PM

Hi Cathal!

 

I would be basically replacing clip art with another chunk of clipart. But…

 

I took a step back and have returned to trying to programmatically adding the original picture. I have it as far as the only thing I need to do after the document is generated to fix it is:

Open document in word

 

Is right click on the image

Select Wrap text

Set it to “Tight”

 

Is there anyway to do that with Docx? I can avoid the whole image replacing if I can do that…

 

Thanks again!