Mat*_*att 5 .net c# docx ms-office
我的项目很紧急,需要我迭代一个大的XML文件并返回Base64编码的图像.
每个图像都必须插入到MS Word文档中,我正在使用DocX库.
我正在将Base64字符串转换为位图,没有任何问题.
对于我的生活,我似乎无法将位图获取到Novacode.Image对象,然后可以将其插入到文档中.注意:我已经知道如何转换为System.Drawing.Image格式.它是Novacode.Image格式(DocX)给我带来的悲伤.
如果我尝试转换(Novacode.Image)somebitmap;我得到的Can not cast expression of type Image to Bitmap.如果我尝试初始化Novacode.Image我得到的新对象Can not access internal constructor Image here.
使用C#,.NET 4,Forms App,很多咖啡.
只有Novacode.Image对象可以通过库插入到MS Word doc中,那么我怎么能在那里得到我的位图?
在这一点上我很沮丧,所以也许我只是错过了一些简单的事情.
Han*_*ans 15
您必须使用该DocX.AddImage()方法来创建Novacode.Image对象.
按照以下5个步骤将图像添加到word文档:
Novacode.Image通过调用AddImage()方法创建对象.CreatePicture()的Novacode.Image对象来创建图片.下面的示例显示了如何将图像插入到Word文档中:
using (DocX doc = DocX.Create(@"Example.docx"))
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Image myImg = System.Drawing.Image.FromFile(@"test.jpg");
myImg.Save(ms, myImg.RawFormat); // Save your picture in a memory stream.
ms.Seek(0, SeekOrigin.Begin);
Novacode.Image img = doc.AddImage(ms); // Create image.
Paragraph p = doc.InsertParagraph("Hello", false);
Picture pic1 = img.CreatePicture(); // Create picture.
pic1.SetPictureShape(BasicShapes.cube); // Set picture shape (if needed)
p.InsertPicture(pic1, 0); // Insert picture into paragraph.
doc.Save();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.