POI操作ppt,合并,转图片
程序员文章站
2022-07-13 12:26:42
...
引入POI
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.0'
compile group: 'batik', name: 'batik-bridge', version: '1.6-1'
compile group: 'org.apache.xmlgraphics', name: 'batik-anim', version: '1.11'
/**
* https://www.w3cschool.cn/apache_poi_ppt/apache_poi_ppt_to_image.html
*/
@Test
public void testMerge() throws Exception{
String file1 = "E:\\pptmerge\\x1.pptx";
String file2 = "E:\\pptmerge\\x2.pptx";
FileInputStream is = new FileInputStream(file1);
XMLSlideShow src = new XMLSlideShow(is);
is.close();
FileInputStream is2 = new FileInputStream(file2);
XMLSlideShow src2 = new XMLSlideShow(is2);
is2.close();
XMLSlideShow ppt = new XMLSlideShow();
for (XSLFSlide slide : src.getSlides()) {
XSLFSlide slide1 = ppt.createSlide(slide.getSlideLayout());
slide1.importContent(slide);
}
for (XSLFSlide slide : src2.getSlides()) {
XSLFSlide slide1 = ppt.createSlide(slide.getSlideLayout());
slide1.importContent(slide);
}
FileOutputStream out = new FileOutputStream("E:\\pptmerge\\merged2.pptx");
ppt.write(out);
out.close();
System.out.println("merge successfully");
}
@Test
public void testPic() throws Exception{
BufferedImage img = new BufferedImage(100, 100,BufferedImage.TYPE_INT_RGB);
img.createGraphics().draw3DRect(0,0,120,100,true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img,"png",out);
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFPictureData pictureData = ppt.addPicture(out.toByteArray(), XSLFPictureData.PictureType.PNG);
XSLFPictureShape picture = slide.createPicture(pictureData);
File file=new File("E:\\pptmerge\\pic.pptx");
FileOutputStream outppt = new FileOutputStream(file);
//saving the changes to a file
ppt.write(outppt);
out.close();
System.out.println("image successfully");
}
@Test
public void testCreatePicFromPPT() throws Exception{
//creating an empty presentation
File file=new File("E:\\pptmerge\\ppt.pptx");
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
//getting the dimensions and size of the slide
Dimension pgsize = ppt.getPageSize();
List<XSLFSlide> slides = ppt.getSlides();
XMLSlideShow pptout = new XMLSlideShow();
for (int i = 0; i < slides.size(); i++) {
XSLFSlide xslfShapes = slides.get(i);
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
//graphics.setPaint(Color.white);
//graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
xslfShapes.draw(graphics);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img,"png",out);
XSLFSlide slide = pptout.createSlide();
XSLFPictureData pictureData = pptout.addPicture(out.toByteArray(), XSLFPictureData.PictureType.PNG);
XSLFPictureShape picture = slide.createPicture(pictureData);
}
//creating an image file as output
FileOutputStream out = new FileOutputStream("E:\\pptmerge\\testCreatePicFromPPT.pptx");
pptout.write(out);
out.close();
System.out.println("Image successfully created");
}
上一篇: java poi ppt操作示例
下一篇: java使用poi读取ppt文件