Retrieving the image name from a powerpoint file
I need to retrieve the image file name from an image in a pptx file. I already got the stream from the image but i didn't got the name of the image file... Here is my code:
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开发者_JS百科 the presentation object.*/
// Setting my custom object ImageName property
paragraph.ImageName = imageName;
// Returning the stream
return stream;
}
Anyone knows how i can accomplish this ? Thanks!!
There are in fact two file names for a picture/image in a pptx file:
If you need the file name of the image as it is embedded in the pptx file you can use the following function:
public static string GetEmbeddedFileName(ImagePart part)
{
return part.Uri.ToString();
}
If you need the orignial file system name of your image you can use the following function:
public static string GetOriginalFileSystemName(DocumentFormat.OpenXml.Presentation.Picture pic)
{
return pic.NonVisualPictureProperties.NonVisualDrawingProperties.Description;
}
BEGIN EDIT:
Here is a complete code example:
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);
}
}
}
END EDIT
Hope, this helps.
精彩评论