我正嘗試使用OpenXML SDK 2.5將圖像添加到現有的Word 2010文檔中。但是當我添加圖像時,圖像不會被嵌入。當我打開文檔時,圖像顯示紅色十字的佔位符(就像無法找到圖像一樣)。 我使用以下代碼:添加了AddImagePart的OpenXML圖像沒有顯示在document.xml.rels中
string mimetype = String.Empty;
// :
//Find mime type of the image here
// :
imagePart = wpd.MainDocumentPart.AddImagePart(mimetype);
using (FileStream stream = new FileStream(filename, FileMode.Open))
{
image = new Bitmap(stream);
cy = Convert.ToUInt32(image.PhysicalDimension.Height);
cx = Convert.ToUInt32(image.PhysicalDimension.Width);
imagePart.FeedData(stream);
stream.Close();
}
Paragraph para = FindParagraphInAppendix(....); //Find the paragraph to add
if (para != null)
AddImageToParagraph(para, wpd.MainDocumentPart.GetIdOfPart(imagePart),txt, filename,cx,cy);
else
wpd.MainDocumentPart.Document.Body.Append(GetImageWithPara(wpd.MainDocumentPart.GetIdOfPart(imagePart), filename, cx, cy));
wpd.Package.CreateRelationship(imagePart.Uri, System.IO.Packaging.TargetMode.External,wpd.MainDocumentPart.GetIdOfPart(imagePart));
wpd.MainDocumentPart.Document.Save();
在代碼爲AddImageToParagraph我添加圖像,如下所示:
Pic.BlipFill blipFill1 = new Pic.BlipFill();
A.Blip blip1 = new A.Blip() { Embed = relationshipid,
CompressionState = A.BlipCompressionValues.Print };
A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();
當我打開使用Winzip生成的文件,則document.xml中.rels文件不包含與嵌入式圖像關聯的關係ID。
當我打開使用OpenXML Productivity工具並驗證XML時,我得到錯誤:「關係'R75a8cc179 ...'被屬性'hxxp://schemas.openxmlformats..../relationships:embed'引用」不存在 你能幫忙嗎?
謝謝!
你可能會發送生成的docx來查看這個問題嗎? – edi9999
無法真正上傳文檔。但是奇怪的是imagePart.AddImagePart不會在生成的document.xml.rels中創建一個''標籤。我看到的所有示例似乎都在做我正在做的事情... –
tr4nc3
其他信息:wpd.GetReferenceRelationship(wpd.MainDocumentPart.GetIdOfPart(imagePart))引發System.Generic.Collections.KeyNotFoundException。 – tr4nc3