2011-10-02 134 views
2

我需要一個PPTX文件從圖像檢索圖像文件名檢索圖像名稱。我已經得到了從圖像的流,但我沒有得到的圖像文件的名稱...這裏是我的代碼:從PowerPoint文件

private static Stream GetParagraphImage(DocumentFormat.OpenXml.Presentation.Picture  picture, DocumentFormat.OpenXml.Packaging.PresentationDocument presentation, ref  MyProject.Import.Office.PowerPoint.Presentation.Paragraph paragraph) 
{ 
     // Getting the image id 
     var imageId = picture.BlipFill.Blip.Embed.Value; 
     // Getting the stream of the image 
     var part = apresentacao.PresentationPart.GetPartById(idImagem); 
     var stream = part.GetStream(); 
     // Getting the image name 
     var imageName = GetImageName(imageId, presentation); 
/* Here i need a method that returns the image file name based on the id of the image and the presentation object.*/ 
     // Setting my custom object ImageName property 
     paragraph.ImageName = imageName; 
     // Returning the stream 
     return stream; 
} 

任何人都知道我是如何做到這一點? 謝謝!

回答

0

有在一個PPTX文件的圖像/影像其實兩個文件名:

如果你需要的圖像的文件名,因爲它是嵌入在PPTX文件 您可以使用以下功能:

public static string GetEmbeddedFileName(ImagePart part) 
{ 
    return part.Uri.ToString(); 
} 

如果你需要你的形象的orignial文件系統名稱,你可以使用下面的功能:

public static string GetOriginalFileSystemName(DocumentFormat.OpenXml.Presentation.Picture pic) 
{ 
    return pic.NonVisualPictureProperties.NonVisualDrawingProperties.Description; 
} 

BE GIN編輯:

下面是一個完整的代碼示例:

using (var doc = PresentationDocument.Open(fileName, false)) 
{ 
    var presentation = doc.PresentationPart.Presentation; 

    foreach (SlideId slide_id in presentation.SlideIdList) 
    { 
    SlidePart slide_part = doc.PresentationPart.GetPartById(slide_id.RelationshipId) as SlidePart; 
    if (slide_part == null || slide_part.Slide == null) 
     continue; 
    Slide slide = slide_part.Slide; 

    foreach (var pic in slide.Descendants<DocumentFormat.OpenXml.Presentation.Picture>()) 
    { 
     string id = pic.NonVisualPictureProperties.NonVisualDrawingProperties.Id; 
     string desc = pic.NonVisualPictureProperties.NonVisualDrawingProperties.Description; 

     Console.Out.WriteLine(desc); 
    } 
    } 
} 

編輯完

希望,這會有所幫助。

+0

嗨,謝謝你的答案,但屬性NonVisualDrawingProperties不直接存在的圖片對象...首先,我需要檢索NonVisualPictureProperties然後NonVisualDrawingProperties.Description,但是當我訪問此屬性時,她返回null ...有是一些使用或其他任何我需要鍵入之前? –

+0

@ mnatan.brito:你說得對。 NonVisualDrawingProperties屬性不直接存在於圖片類中。我用更完整的代碼示例更新了我的答案。我已經再次運行了一個power point 2007文檔,它打印了原始文件名。希望這可以幫助。 – Hans

+0

描述屬性爲所有圖像返回null在PPTX ... –