将现有提取的图像添加到幻灯片
Adding existing extracted images to slides
我正在寻找一种方法来获取提取的现有图像并将它们添加到新的 PowerPoint 项目中。该程序应获取现有图像,将它们保存到文件夹或位置,然后其余代码应获取它们并将它们添加到正确格式化到每张幻灯片上的 PPTX。
请查看下面我的 Java 代码:
// Converting the slides of a PPT into Images using Java
import java.util.List;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import javax.imageio.ImageIO;
public class Array {
public static void main(String args[])
throws IOException {
// create an empty presentation
File file = new File("PowerPoint Location");
XMLSlideShow ppt
= new XMLSlideShow(new FileInputStream(file));
// get the dimension and size of the slide
Dimension pgsize = ppt.getPageSize();
List < XSLFSlide > slide = ppt.getSlides();
BufferedImage img = null;
System.out.println(slide.size());
for (int i = 0; i < slide.size(); i++) {
img = new BufferedImage(
pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// clear area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(
0, 0, pgsize.width, pgsize.height));
// draw the images
slide.get(i).draw(graphics);
FileOutputStream out = new FileOutputStream(
"ppt_image" + i + ".png");
ImageIO.write(img, "png", out);
ppt.write(out);
out.close();
System.out.println(i);
}
System.out.println("Images successfully created");
//creating a presentation
//creating a slide in it
//reading an image
File image = new File("Take in existing images to add to new project/presentation");
//converting it into a byte array
byte[] picture = IOUtils.toByteArray(new FileInputStream(image));
//adding the image to the presentation
XSLFPictureData idx = ppt.addPicture(picture, PictureData.PictureType.PNG);
//creating a slide with given picture on it
XSLFPictureShape pic = slide.createPicture(idx);
//creating a file object
File fileTwo = new File("Create new presentation with slides formatted on each slide");
FileOutputStream out = new FileOutputStream(file);
//saving the changes to a file
ppt.write(out);
System.out.println("image added successfully");
out.close();
}
}
从你的问题中并不清楚你到底想做什么。根据代码,您似乎希望从输入幻灯片中提取所有幻灯片作为图片,并创建一个新的输出幻灯片,每个提取的图片都有一张幻灯片。最简单的代码如下:
import java.util.List;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.*;
import javax.imageio.ImageIO;
public class PowerPointExtractSliderImagesToNewPPTX {
public static void main(String[] args) throws Exception {
String filePathIn = "./PowerPoint.pptx"; // input PPTX
String filePathOut = "./PowerPointNew.pptx"; // output PPTX
FileInputStream in = new FileInputStream(filePathIn);
XMLSlideShow pptIn = new XMLSlideShow(in); // input slideshow
XMLSlideShow pptOut = new XMLSlideShow(); // output slideshow
// get the dimension of the slide in input PPTX
Dimension pgsize = pptIn.getPageSize();
// get the slides from input slideshow
List <XSLFSlide> slides = pptIn.getSlides();
System.out.println(slides.size());
// get all slides from input slideshow as pictures and create a slide in output slideshow for each picture
for (int i = 0; i < slides.size(); i++) {
//buffered image in page size
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
// clear area
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// draw the slide as image into a byte[]
slides.get(i).draw(graphics);
ByteArrayOutputStream pictureBytesOut = new ByteArrayOutputStream();
ImageIO.write(img, "png", pictureBytesOut);
byte[] pictureBytes = pictureBytesOut.toByteArray();
//adding the image to the output slideshow
XSLFPictureData idx = pptOut.addPicture(pictureBytes, PictureData.PictureType.PNG);
//creating a slide with given picture on it in output slideshow
XSLFSlide slide = pptOut.createSlide();
XSLFPictureShape pic = slide.createPicture(idx);
}
pptIn.close();
// write the output slideshow
FileOutputStream out = new FileOutputStream(filePathOut);
pptOut.write(out);
out.close();
pptOut.close();
}
}
我正在寻找一种方法来获取提取的现有图像并将它们添加到新的 PowerPoint 项目中。该程序应获取现有图像,将它们保存到文件夹或位置,然后其余代码应获取它们并将它们添加到正确格式化到每张幻灯片上的 PPTX。
请查看下面我的 Java 代码:
// Converting the slides of a PPT into Images using Java
import java.util.List;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import javax.imageio.ImageIO;
public class Array {
public static void main(String args[])
throws IOException {
// create an empty presentation
File file = new File("PowerPoint Location");
XMLSlideShow ppt
= new XMLSlideShow(new FileInputStream(file));
// get the dimension and size of the slide
Dimension pgsize = ppt.getPageSize();
List < XSLFSlide > slide = ppt.getSlides();
BufferedImage img = null;
System.out.println(slide.size());
for (int i = 0; i < slide.size(); i++) {
img = new BufferedImage(
pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// clear area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(
0, 0, pgsize.width, pgsize.height));
// draw the images
slide.get(i).draw(graphics);
FileOutputStream out = new FileOutputStream(
"ppt_image" + i + ".png");
ImageIO.write(img, "png", out);
ppt.write(out);
out.close();
System.out.println(i);
}
System.out.println("Images successfully created");
//creating a presentation
//creating a slide in it
//reading an image
File image = new File("Take in existing images to add to new project/presentation");
//converting it into a byte array
byte[] picture = IOUtils.toByteArray(new FileInputStream(image));
//adding the image to the presentation
XSLFPictureData idx = ppt.addPicture(picture, PictureData.PictureType.PNG);
//creating a slide with given picture on it
XSLFPictureShape pic = slide.createPicture(idx);
//creating a file object
File fileTwo = new File("Create new presentation with slides formatted on each slide");
FileOutputStream out = new FileOutputStream(file);
//saving the changes to a file
ppt.write(out);
System.out.println("image added successfully");
out.close();
}
}
从你的问题中并不清楚你到底想做什么。根据代码,您似乎希望从输入幻灯片中提取所有幻灯片作为图片,并创建一个新的输出幻灯片,每个提取的图片都有一张幻灯片。最简单的代码如下:
import java.util.List;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.*;
import javax.imageio.ImageIO;
public class PowerPointExtractSliderImagesToNewPPTX {
public static void main(String[] args) throws Exception {
String filePathIn = "./PowerPoint.pptx"; // input PPTX
String filePathOut = "./PowerPointNew.pptx"; // output PPTX
FileInputStream in = new FileInputStream(filePathIn);
XMLSlideShow pptIn = new XMLSlideShow(in); // input slideshow
XMLSlideShow pptOut = new XMLSlideShow(); // output slideshow
// get the dimension of the slide in input PPTX
Dimension pgsize = pptIn.getPageSize();
// get the slides from input slideshow
List <XSLFSlide> slides = pptIn.getSlides();
System.out.println(slides.size());
// get all slides from input slideshow as pictures and create a slide in output slideshow for each picture
for (int i = 0; i < slides.size(); i++) {
//buffered image in page size
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
// clear area
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// draw the slide as image into a byte[]
slides.get(i).draw(graphics);
ByteArrayOutputStream pictureBytesOut = new ByteArrayOutputStream();
ImageIO.write(img, "png", pictureBytesOut);
byte[] pictureBytes = pictureBytesOut.toByteArray();
//adding the image to the output slideshow
XSLFPictureData idx = pptOut.addPicture(pictureBytes, PictureData.PictureType.PNG);
//creating a slide with given picture on it in output slideshow
XSLFSlide slide = pptOut.createSlide();
XSLFPictureShape pic = slide.createPicture(idx);
}
pptIn.close();
// write the output slideshow
FileOutputStream out = new FileOutputStream(filePathOut);
pptOut.write(out);
out.close();
pptOut.close();
}
}