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

北城余情 提交于 2019-11-30 08:34:31

问题


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.


回答1:


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;



回答2:


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)



来源:https://stackoverflow.com/questions/8483471/how-to-change-the-size-of-a-picture-after-inserting-it-into-a-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!