簡體   English   中英

OpenXml多個圖像

[英]OpenXml Multiple Images

我目前正在使用“圖片內容控件”插入圖像,但是(根據控件的性質)似乎存在明顯的限制,即只有1張IMAGE。

如何使用OpenXML SDK(2+)在設置的位置添加多個圖像?

我確實嘗試過Bookmarks,但似乎沒有用,只是導致文檔損壞。
我已經有很多代碼可以構建現有文檔,因此考慮使用mHtml路由不是一種選擇。
最后,我確實嘗試了OpenXML SDK生產力工具,但是仍然看不到如何在設置的位置插入多個圖像。

問題在於圖片內容控件都具有一些指向同一“空白”圖像的ID。 您必須將每個圖像的資源ID分配給每個內容控件的blip.embed屬性。

這篇文章為您提供了一種簡單有效的方法

Open XML –通過標簽名稱設置多個圖片內容控件,而不會發瘋

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM