C# OpenXMLSDK:编辑后嵌入式OLE对象收缩
我必须创建一个非常大的Word文档,其中包含许多绘图(是的,它必须是Word)。绘图由第三方应用程序生成。由于第三方应用程序只支持小于8MIB的Word文档,并且我必须生成的文档要大得多,因此我将图片打印到单个文档中,每个文档一个,然后提取它们,并将它们添加到最终文档中 他们使用的“图片格式”是一些通过OLE嵌入的专有内容。双击它会在后台打开一个应用程序,允许在图片中进行缩放和平移。现在,当离开图片编辑器时,图片突然缩小,破坏了文档的布局。我从中提取图片的原始文档没有显示这种行为 目前,我所做的是:C# OpenXMLSDK:编辑后嵌入式OLE对象收缩,c#,openxml,openxml-sdk,C#,Openxml,Openxml Sdk,我必须创建一个非常大的Word文档,其中包含许多绘图(是的,它必须是Word)。绘图由第三方应用程序生成。由于第三方应用程序只支持小于8MIB的Word文档,并且我必须生成的文档要大得多,因此我将图片打印到单个文档中,每个文档一个,然后提取它们,并将它们添加到最终文档中 他们使用的“图片格式”是一些通过OLE嵌入的专有内容。双击它会在后台打开一个应用程序,允许在图片中进行缩放和平移。现在,当离开图片编辑器时,图片突然缩小,破坏了文档的布局。我从中提取图片的原始文档没有显示这种行为 目前,我所做的
temp.docx
temp.docx
作为WordprocessingDocument
plot.bin
plot.WMF
Shape
对象,创建其Shapetype
子对象的克隆plot.bin
添加为EmbeddedObjectPart
plot.wmf
添加为ImagePart
plot.wmf
id的ImageData
子对象创建Shape
嵌入对象添加到段落中
/// <summary>
/// Adds a picture
/// </summary>
/// <param name="doc">Document to add picture to</param>
/// <param name="pictureFile">File with proprietary picture data</param>
/// <param name="wmfRepresentationFile">WMF representation of the proprietary picture file</param>
/// <param name="pictureNumber">A rising number of the embedded picture. Incremented upon
/// embedding the picture!</param>
private static void AddPicture(WordprocessingDocument doc,
string pictureFile,
string wmfRepresentationFile,
Shapetype shapeType,
ref int pictureNumber) {
LOG.Debug("Embedding the picture in the document");
var emObj = doc.MainDocumentPart.AddEmbeddedObjectPart("application/vnd.openxmlformats-officedocument.oleObject");
using (var pictureIn = new FileStream(pictureFile, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var oleOut = emObj.GetStream(FileMode.OpenOrCreate, FileAccess.Write)) {
oleOut.Position = 0;
pictureIn.CopyTo(oleOut);
oleOut.Flush();
}
// An OLE embedded object also needs an image part.
var image = doc.MainDocumentPart.AddImagePart(ImagePartType.Wmf);
using (var imageIn = new FileStream(wmfRepresentationFile, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var imageOut = image.GetStream(FileMode.OpenOrCreate, FileAccess.Write)) {
imageOut.Position = 0;
imageIn.CopyTo(imageOut);
imageOut.Flush();
}
// Get some IDs we need for the linking
var objectPartId = doc.MainDocumentPart.GetIdOfPart(emObj);
var objectId = ("_" + emObj.GetHashCode().ToString() + "000000").Substring(0, 11);
var imagePartId = doc.MainDocumentPart.GetIdOfPart(image);
// Create a new paragraph with the embedded object.
// First, a "Shape"
var shape = new Shape() {
Id = "_x0000_i10" + pictureNumber,
Style = "width:500.0pt;height:600.0pt", // Sets the size of the picture in the document
Ole = new TrueFalseBlankValue(),
Type = "#_x0000_t75",
};
// This must contain the "imagedata" - a link to the image representation of the
// empty active picture
shape.AppendChild(new ImageData() {
Title = "",
RelationshipId = imagePartId
});
// Now, the link to the active picture itself
var ole = new OleObject() {
Type = OleValues.Embed,
ProgId = "Foo.Proprietary.Picture.1",
ShapeId = "_x0000_i10" + pictureNumber,
DrawAspect = OleDrawAspectValues.Content,
ObjectId = objectId,
Id = objectPartId
};
// Now, wrap everything into a Paragraph / Run / EmbeddedObject
var pictureParagraph = new Paragraph(new Run(new EmbeddedObject(shapeType, shape, ole)));
// And append it
doc.MainDocumentPart.Document.Body.AppendChild(pictureParagraph);
// Increase the image counter
pictureNumber++;
}
//
///添加图片
///
///要将图片添加到的文档
///带有专有图片数据的文件
///专有图片文件的WMF表示法
///嵌入图片的数量不断增加。增加
///嵌入图片!
私有静态无效添加图片(WordprocessingDocument文档,
字符串图片文件,
字符串wmfRepresentationFile,
形状类型形状类型,
ref int pictureNumber){
LOG.Debug(“将图片嵌入文档”);
var emObj=doc.MainDocumentPart.AddEmbeddedObjectPart(“application/vnd.openxmlformats of cedocument.oleObject”);
使用(var pictureIn=newfilestream(pictureFile,FileMode.Open,FileAccess.Read,FileShare.Read))
使用(var oleOut=emObj.GetStream(FileMode.OpenOrCreate,FileAccess.Write)){
位置=0;
pictureIn.CopyTo(oleOut);
oleOut.Flush();
}
//OLE嵌入对象还需要图像部分。
var image=doc.MainDocumentPart.AddImagePart(ImagePartType.Wmf);
使用(var imageIn=newfilestream(wmfRepresentationFile,FileMode.Open,FileAccess.Read,FileShare.Read))
使用(var imageOut=image.GetStream(FileMode.OpenOrCreate,FileAccess.Write)){
imageOut.Position=0;
imageIn.CopyTo(imageOut);
imageOut.Flush();
}
//获取链接所需的一些ID
var objectPartId=doc.MainDocumentPart.GetIdOfPart(emObj);
var objectId=(“”+emObj.GetHashCode().ToString()+“000000”).Substring(0,11);
var imagePartId=doc.MainDocumentPart.GetIdOfPart(图像);
//使用嵌入对象创建新段落。
//首先,一个“形状”
var shape=新形状(){
Id=“\u x0000\u i10”+图片编号,
Style=“宽度:500.0pt;高度:600.0pt”,//设置文档中图片的大小
Ole=新的TrueFalseBlankValue(),
Type=“#ux0000_t75”,
};
//这必须包含“imagedata”-指向图像表示的链接
//空活动图片
AppendChild(新的ImageData(){
Title=“”,
RelationshipId=imagePartId
});
//现在,链接到活动图片本身
var ole=new OleObject(){
类型=OleValues.Embed,
ProgId=“Foo.propertial.Picture.1”,
ShapeId=“\u x0000\u i10”+图片编号,
DrawAspect=OleDrawAspectValues.Content,
ObjectId=ObjectId,
Id=objectPartId
};
//现在,将所有内容包装到段落/Run/EmbeddedObject中
var pictureParagraph=新段落(新运行(新嵌入对象(shapeType、shape、ole));
//并附加它
doc.MainDocumentPart.Document.Body.AppendChild(pictureParagraph);
//增加图像计数器
pictureNumber++;
}
我错过了什么/做错了什么