Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the size of a picture after inserting it into a word document

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.

like image 976
Tired Nerd Avatar asked Dec 13 '11 02:12

Tired Nerd


People also ask

Why can't I change the size of an image in Word?

Click the picture you want to resize. Go to the Picture Format tab, and then click "Position" > "More Layout Options." Click the "Size" tab, and then in the "Scale" section, make sure the "Lock Aspect Ratio" checkbox is clear.

Can we edit the image after we insert it in a Word document?

Click the "Edit" tab to open the image editing. You can right-click the image and then you can cut, copy, paste and rotate. The size can also be adjusted as per needs and demand.


2 Answers

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;
like image 80
John Koerner Avatar answered Sep 20 '22 13:09

John Koerner


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)

like image 36
Peter Avatar answered Sep 19 '22 13:09

Peter