2011-08-15 12 views
5

Tôi bắt đầu thử nghiệm với Open XML SDK 2.0 for Microsoft Office.Tôi làm cách nào để truy xuất hình ảnh từ tệp .pptx bằng cách sử dụng MS Open XML SDK?

Tôi hiện có thể thực hiện những việc nhất định như truy xuất tất cả văn bản trong mỗi trang trình bày và nhận được kích thước của bản trình bày. Ví dụ: tôi làm theo cách này theo cách này:

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

Bây giờ tôi muốn truy xuất hình ảnh được nhúng trong một trang trình bày nhất định. Có ai biết làm thế nào để làm điều này hoặc có thể chỉ cho tôi một số tài liệu về chủ đề này?

+0

Tôi tò mò - tại sao "/ 9525.0"? Bộ chia chuẩn cho EMU-to-point là "/ 12700". –

Trả lời

1

Trước tiên, bạn cần phải lấy SlidePart mà bạn muốn để có được những hình ảnh từ:

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

Sau đó, bạn cần phải tìm kiếm các đối tượng Picture mà sẽ chứa các hình ảnh mà bạn đang tìm kiếm dựa trên các tập tin tên của hình ảnh:

Picture imageToRemove = slidePart.Slide.Descendants<Picture>().SingleOrDefault(picture => picture.NonVisualPictureProperties.OuterXml.Contains(imageFileName)); 
+0

Làm thế nào để chuyển đổi SlidePart thành một hình ảnh thực tế có thể có trong imageList? –

+1

Mã này dường như giả định rằng bạn biết tên tệp của hình ảnh - phải không? Điều gì sẽ xảy ra nếu tôi chỉ muốn truy xuất hình ảnh đầu tiên trong tệp PPTX hoặc tất cả hình ảnh trong tệp PPTX? –

+0

Có cách nào để chuyển đổi tất cả các slide (s) thành hình ảnh (s) hoặc svg? – sridharnetha

0

cách đơn giản nhất để nhận được hình ảnh từ các định dạng OpenXML:

Sử dụng bất kỳ thư viện lưu trữ zip nào để trích xuất hình ảnh từ thư mục phương tiện của tệp pptx. Điều này sẽ chứa các hình ảnh trong tài liệu. Tương tự, bạn có thể thay thế thủ công phần mở rộng .pptx thành tệp .zip và trích xuất để lấy hình ảnh từ thư mục phương tiện.

Hy vọng điều này sẽ hữu ích.