How to resize an image after pasting it into a text document

I am adding an image to a text document in a specific bookmark. However, the image is too large and causes the text to leave the page, so I need to resize the image after it is in the word document.

+6
source share
2 answers

When you insert an image, it should return an InlineShape that you can change:

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; 
+17
source

The code I used to resize the image:

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

Converting to Shape seems to be the solution. The previous set of different heights directly in InLineShapes caused an error. (I just edited the post and simplified the code, so it doesn't use the second dll library: Microsoft.Office.Core)

0
source

Source: https://habr.com/ru/post/903642/


All Articles