我有以下代码从.pptx文件中提取图像:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
代码有效,但只从.ppt文件中提取一些随机图像。如何按顺序提取所有图像?
答案 0 :(得分:1)
看起来它不是随机图像,而是第一个。尝试使用foreach循环迭代presentationPart.GetPartsOfType<SlidePart>()
和slidePart.GetPartsOfType<ImagePart>()
。
编辑:
这是所有图像的工作代码。它生成随机文件名:
public static void GetImages()
{
var doc = PresentationDocument.Open(@"E:\Dev\Stuff\ConsoleApplication1\ConsoleApplication1\bin\Debug\test.pptx", true);
var presentationPart = doc.PresentationPart;
foreach (var slide in presentationPart.GetPartsOfType<SlidePart>())
{
foreach (var image in slide.GetPartsOfType<ImagePart>())
{
if (image != null)
{
var stream = image.GetStream();
var img = Image.FromStream(stream);
img.Save(@"E:\Dev\Stuff\ConsoleApplication1\ConsoleApplication1\bin\Debug\" + System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(), "jpg"));
}
}
}
}
答案 1 :(得分:1)
我做了一些手动迭代,但同样,我没有得到预期的图像。在这个例子中,它应该从第6张幻灯片返回第一张图片,但它会从不同的幻灯片返回一些其他图片:
public static void GetImages()
{
var doc = PresentationDocument.Open(@"D:\Peak Sourcing\Work\ppt_test\xx.pptx", true);
var presentationPart = doc.PresentationPart;
int number_of_slides = presentationPart.GetPartsOfType<SlidePart>().Count(); //get number of slides in .ppt
var slidePart = presentationPart.GetPartsOfType<SlidePart>().ElementAt(5); //look for image inside this slide number + 1
int number_of_images = slidePart.GetPartsOfType<ImagePart>().Count(); //get number of images in current slide
var imagePart = slidePart.GetPartsOfType<ImagePart>().ElementAt(0); //should return first image of selected slide, but it doesn't....it returns an image from some other slide
var stream = imagePart.GetStream();
var img = Image.FromStream(stream);
img.Save(@"D:\Peak Sourcing\Work\ppt_test\test-output.jpg");
}