通过poi操作ppt中的图片
生活随笔
收集整理的這篇文章主要介紹了
通过poi操作ppt中的图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//完整添加圖片
public class MyFirstPPTTest {public static void main(String[] args) throws Exception{/** 文件路徑 **/String filePath = "D://MyPPT.pptx";String imagePath = "D://a.png";String imagePath2 = "D://b.png";String imagePath3 = "D://c.png";/** 加載PPT **/XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filePath));/** 創(chuàng)建一個slide,理解為PPT里的每一頁 **/XSLFSlide slide = ppt.createSlide();XSLFSlide slide2 = ppt.createSlide();XSLFSlide slide3 = ppt.createSlide();/** 生成二進制數(shù)組 **/byte[] pictureData = IOUtils.toByteArray(new FileInputStream(imagePath));byte[] pictureData2 = IOUtils.toByteArray(new FileInputStream(imagePath2));byte[] pictureData3 = IOUtils.toByteArray(new FileInputStream(imagePath3));/** 添加圖片,返回索引 **/int pictureIndex = ppt.addPicture(pictureData, XSLFPictureData.PICTURE_TYPE_PNG);int pictureIndex2 = ppt.addPicture(pictureData2, XSLFPictureData.PICTURE_TYPE_PNG);int pictureIndex3 = ppt.addPicture(pictureData3, XSLFPictureData.PICTURE_TYPE_PNG);/** 打印信息 **/System.out.println("pictureIndex " + pictureIndex);// pictureIndex 0System.out.println("pictureIndex2 " + pictureIndex2);// pictureIndex2 1System.out.println("pictureIndex3 " + pictureIndex3);// pictureIndex3 2/** 創(chuàng)建圖片 **/XSLFPictureShape pictureShape = slide.createPicture(pictureIndex);XSLFPictureShape pictureShape2 = slide2.createPicture(pictureIndex2);XSLFPictureShape pictureShape3 = slide3.createPicture(pictureIndex3);/** 設(shè)置圖片的位置 四個參數(shù)分別為 x y width height **/pictureShape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300));pictureShape2.setAnchor(new java.awt.Rectangle(50, 150, 500, 300));pictureShape3.setAnchor(new java.awt.Rectangle(50, 250, 500, 300));/** 獲取圖片類別 **/int pictureType = pictureShape.getPictureData().getPictureType();switch (pictureType){case Picture.JPEG:System.out.println("the type of picture is : " + "JPEG");break;case Picture.PNG:System.out.println("the type of picture is :" + "PNG");break;}System.out.println(pictureType);/** 輸出文件 **/ppt.write(new FileOutputStream(filePath));}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/cww0814/p/8378027.html
總結(jié)
以上是生活随笔為你收集整理的通过poi操作ppt中的图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces 617E XOR
- 下一篇: ldconfig与 /etc/ld.so