c# VSTO
如何在 word 2010 的富文本内容控件中插入带有一些标题的图像。属性 formattedtext 甚至不接受范围对象
将图像添加到富文本内容控件非常简单,您只需将其添加到Range.InlineShapes
集合中即可。
要从 Word 文档中检索图像,您可以执行以下操作:
这是一个例子:
添加对 System.Windows.Forms.dll 的引用以允许使用剪贴板类
string imagePath = @"D:\microsoft.jpg";
string title = "Microsoft";
Word.ContentControls cc = this.Application.ActiveDocument.ContentControls;
//Add a rich content control,add an image to the rich content control
var richContentControl = cc.Add(Word.WdContentControlType.wdContentControlRichText);
richContentControl.Range.FormattedText.Text = title;
if (System.IO.File.Exists(imagePath))
{
Word.InlineShape image = richContentControl.Range.InlineShapes.AddPicture(imagePath);
image.Height = 70;
image.Width = 100;
//Retrieve all images from content controls and save to disk
foreach (Word.ContentControl c in cc)
{
if (c.Range.InlineShapes.Count > 0)
{
foreach (Word.InlineShape shape in c.Range.InlineShapes)
{
shape.Range.Copy();
if (System.Windows.Forms.Clipboard.ContainsImage())
{
System.Drawing.Image clipboardImage = Clipboard.GetImage();
string path = System.IO.Path.Combine(@"D:\imageName.jpg");
clipboardImage.Save(path);
}
Clipboard.Clear();
}
}
}
}