8

I'm adding a picture to a word document at a certain bookmark. However, the picture is too big and is forcing text off the page, so I need to be able to change the size of the picture after it is in the word document.

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Tired Nerd
  • 93
  • 1
  • 1
  • 3

2 Answers2

24

When you insert the image, it should return you an InlineShape, which you can modify:

Word.Application app = new Word.Application();
var doc = app.Documents.Open(@"C:\Users\SomeUserName\Desktop\Doc1.docx");

var shape = doc.Bookmarks["PicHere"].Range.InlineShapes.AddPicture(@"C:\Users\SomePicture\Pictures\1234.JPG", false, true);
shape.Width = 150;
shape.Height = 150;
app.Visible = true;
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • 3
    Awesome, works like a charm. I could not figure that one out for some reason. I'm gonna go bang my head against a wall now.. – Tired Nerd Dec 13 '11 at 06:24
1

Code, which I used to resize the picture successfully is:

var shape = headerRange.InlineShapes.AddPicture(tempLogoPathName, true, true).ConvertToShape();
shape.HeightRelative = 10f;
shape.WidthRelative = 40f;

Seems that convering to Shape is the solution. Previous set the different height directly in InLineShapes, produced an error. (I just edited a post and simplified the code, so it does not use 2nd dll library: Microsoft.Office.Core anymore)

Peter
  • 11
  • 3
  • Is this an answer or a question or both? If you need to ask something related please make a new question instead appending questions to your answer. – pirho Dec 08 '17 at 12:11