![](/img/trans.png)
[英]How can I retrieve some image data and format using MS Open XML SDK?
[英]How can I retrieve images from a .pptx file using MS Open XML SDK?
我開始嘗試使用Open XML SDK 2.0 for Microsoft Office 。
我目前能夠做某些事情,例如檢索每張幻燈片中的所有文本,並獲取演示文稿的大小。 例如,我這樣做后者:
using (var doc = PresentationDocument.Open(pptx_filename, false)) {
var presentation = doc.PresentationPart.Presentation;
Debug.Print("width: " + (presentation.SlideSize.Cx / 9525.0).ToString());
Debug.Print("height: " + (presentation.SlideSize.Cy / 9525.0).ToString());
}
現在我想檢索給定幻燈片中的嵌入圖像。 有誰知道如何做到這一點,或者可以向我指出有關該主題的一些文檔?
首先,您需要獲取要從中獲取圖像的SlidePart
:
public static SlidePart GetSlidePart(PresentationDocument presentationDocument, int slideIndex)
{
if (presentationDocument == null)
{
throw new ArgumentNullException("presentationDocument", "GetSlidePart Method: parameter presentationDocument is null");
}
// Get the number of slides in the presentation
int slidesCount = CountSlides(presentationDocument);
if (slideIndex < 0 || slideIndex >= slidesCount)
{
throw new ArgumentOutOfRangeException("slideIndex", "GetSlidePart Method: parameter slideIndex is out of range");
}
PresentationPart presentationPart = presentationDocument.PresentationPart;
// Verify that the presentation part and presentation exist.
if (presentationPart != null && presentationPart.Presentation != null)
{
Presentation presentation = presentationPart.Presentation;
if (presentation.SlideIdList != null)
{
// Get the collection of slide IDs from the slide ID list.
var slideIds = presentation.SlideIdList.ChildElements;
if (slideIndex < slideIds.Count)
{
// Get the relationship ID of the slide.
string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;
// Get the specified slide part from the relationship ID.
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
return slidePart;
}
}
}
// No slide found
return null;
}
然后您需要根據圖像的文件名搜索包含您要查找的圖像的Picture
object:
Picture imageToRemove = slidePart.Slide.Descendants<Picture>().SingleOrDefault(picture => picture.NonVisualPictureProperties.OuterXml.Contains(imageFileName));
從 Openxml 格式獲取圖像的最簡單方法:
使用任何 zip 存檔庫從 pptx 文件的媒體文件夾中提取圖像。 這將包含文檔中的圖像。 同樣,您可以手動將 extension.pptx 替換為.zip 並提取以從媒體文件夾中獲取圖像。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.