我目前正在使用Picture Content控件插入图像,但似乎有一个明显的限制(根据控件的性质),只能插入一个图像。
如何使用OpenXML SDK (2+)在设置的位置添加多个镜像?
我确实尝试过BookMarks,但似乎不起作用,只是导致文档损坏。
我已经有相当多用于构建现有文档的代码,因此不能考虑使用mHtml路由。
最后,我确实尝试了OpenXML SDK生产力工具,但仍然不知道如何在设置的位置插入多个图像。
发布于 2016-05-27 01:11:00
问题是所有的图片内容控件都有一些id指向相同的“空白”图像。必须将每个图像的资源id分配给每个content控件的blip.embed属性
这篇文章给了你一个简短而有效的方法
Open XML – Setting multiple Picture Content Controls by Tag name, without going crazy
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
// Select element containing picture control and get the blip element
Bitmap image = new Bitmap(@"F:insert_me.jpg");
SdtElement controlBlock = _mainDocumentPart.Document.Body
.Descendants<SdtElement>()
.Where
(r =>
r.SdtProperties.GetFirstChild<Tag>().Val == tagName
).SingleOrDefault();
// Find the Blip element of the content control.
A.Blip blip = controlBlock.Descendants<A.Blip>().FirstOrDefault();
// Add image and change embeded id.
ImagePart imagePart = _mainDocumentPart
.AddImagePart(ImagePartType.Jpeg);
using (MemoryStream stream = new MemoryStream())
{
image.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
imagePart.FeedData(stream);
}
blip.Embed = _mainDocumentPart.GetIdOfPart(imagePart);https://stackoverflow.com/questions/16707041
复制相似问题