我创建了一个带有图片占位符的doc文件。现在我需要在占位符中插入图像。我使用以下代码。
using Microsoft.Office.Interop.Word;
using System.Drawing;
namespace ImagetoDoc
{
class Program
{
static void Main(string[] args)
{
Image im = Image.FromFile(@"C:\Users\BabyboB\Documents\google.png");
Application app= new Application();
Document doc = app.Documents.Open(@"C:\Users\BabyboB\Documents\testingdoc.docx");
doc.SelectContentControlsByTag("testing");
}
}
}
识别标签后如何添加图片?
答案 0 :(得分:0)
尝试以下
const string FILE_IMAGE = @"C:\Users\BabyboB\Documents\google.png";
const string FILE_DOCX = @"C:\Users\BabyboB\Documents\testingdoc.docx";
var app = new MsWord.Application();
MsWord.Document doc = null;
try
{
doc = app.Documents.Open(FILE_DOCX, Type.Missing);
var testingCtrls = doc.SelectContentControlsByTag("testing");
//assuming image jas to be added to 1st control in testingCtrls
//it should be a content control which allows picture in it.
//e.g. wdContentControlRichText or wdContentControlPicture.
var testingCtrl = testingCtrls[1];
testingCtrl.Range.InlineShapes.AddPicture(FILE_IMAGE, Type.Missing, Type.Missing, Type.Missing);
doc.Save();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}