2011-10-02 132 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 ... –