1)若是是建立新的PPT文檔,直接使用SlideShow和Slide類就能夠,其中SlideShow表示PPT文檔,Slide表示某一張幻燈片
以下代碼建立空的PPT文檔:java
1 SlideShow ppt = new SlideShow(); 2 Slide[] slides = ppt.getSlides(); 3 assertTrue(slides.length == 0); 4 savePPTFile(ppt); 5 6 private void savePPTFile(SlideShow ppt) throws Exception{ 7 FileOutputStream out = new FileOutputStream("ppt測試.ppt"); 8 ppt.write(out); 9 out.close(); 10 }
2)設置母版,這樣後續的新建幻燈片都將使用母版的字體,背景等設置web
1 SlideShow ppt = new SlideShow(); 2 //設置幻燈片大小 3 ppt.setPageSize(new Dimension(760,600)); 4 SlideMaster master = ppt.getSlidesMasters()[0]; 5 //設置母板背景,支持多種圖片格式 6 int picIndex = ppt.addPicture(new File("background.png"), Picture.PNG); 7 Picture background = new Picture(picIndex); 8 //設置圖片位置 9 background.setAnchor(new java.awt.Rectangle(0, 0, ppt.getPageSize().width 10 , ppt.getPageSize().height)); 11 master.addShape(background);
3)建立幻燈片並插入文本ide
1 SlideShow ppt = new SlideShow(); 2 Slide newSlide = ppt.createSlide(); 3 4 //添加幻燈片標題 5 TextBox title = newSlide.addTitle(); 6 RichTextRun titleRun = title.getTextRun().getRichTextRuns()[0]; 7 titleRun.setFontColor(Color.RED); 8 title.setText("ppt測試"); 9 10 //添加文本框 11 TextBox txt = new TextBox(); 12 RichTextRun richTextRun = txt.getTextRun().getRichTextRuns()[0]; 13 richTextRun.setFontColor(Color.BLUE); 14 //setText參數字符串能夠包含回車、換行符,可是最後一行不能以\r\n結尾,不然設置的格式沒有效果(v3.5) 15 richTextRun.setText("這裏能夠換行\r\n第二行文本"); 16 txt.setAnchor(new java.awt.Rectangle(50,150,400,400)); 17 newSlide.addShape(txt); 18 19 savePPTFile(ppt);
4)插入圖片,支持多種格式測試
1 SlideShow ppt = new SlideShow(); 2 Slide newSlide = ppt.createSlide(); 3 int picIndex = ppt.addPicture(new File("圖片.jpg"), Picture.JPEG); 4 Picture jpg = new Picture(picIndex); 5 6 //set image position in the slide 7 jpg.setAnchor(new java.awt.Rectangle(360, 200, 280, 260)); 8 9 newSlide.addShape(jpg); 10 savePPTFile(ppt);
5)插入表格(v3.5)字體
1 SlideShow ppt = new SlideShow(); 2 Slide slide = ppt.createSlide(); 3 4 String[][] datas = { 5 {"序號", "姓名","年齡"}, 6 {"1", "張三","30"}, 7 {"2", "李四","27"}, 8 }; 9 10 //create a table of 3 rows and 3 columns 11 Table table = new Table(3, 3); 12 13 for (int i = 0; i < datas.length; i++) { 14 for (int j = 0; j < datas[i].length; j++) { 15 TableCell cell = table.getCell(i, j); 16 17 RichTextRun rt = cell.getTextRun().getRichTextRuns()[0]; 18 rt.setFontName("宋體"); 19 rt.setFontSize(12); 20 21 cell.setVerticalAlignment(TextBox.AnchorMiddle); 22 cell.setHorizontalAlignment(TextBox.AlignCenter); 23 cell.setText(datas[i][j]); 24 25 if(i == 0){//首行背景設置爲灰色 26 cell.setFillColor(Color.GRAY); 27 } 28 } 29 } 30 31 Line border = table.createBorder(); 32 border.setLineColor(Color.black); 33 border.setLineWidth(2.0); 34 table.setAllBorders(border); 35 36 slide.addShape(table); 37 table.moveTo(160,260); 38 savePPTFile(ppt);
6)若是是讀取已存在的PPT文檔則還要用到HSLFSlideShow,下面代碼將PPT文件導出爲圖片(png)格式,若是幻燈片上有中文字符則這些字符的字體須要修改成支持中文的字體(宋體等),不然導出的圖片的中文字符不能正常顯示spa
1 SlideShow ppt = new SlideShow(new HSLFSlideShow("PPT測試.ppt")); 2 Dimension pgsize = ppt.getPageSize(); 3 Slide[] slide = ppt.getSlides(); 4 5 for (int i = 0; i < slide.length; i++) { 6 BufferedImage img = new BufferedImage(pgsize.width, pgsize.height 7 , BufferedImage.TYPE_INT_RGB); 8 Graphics2D graphics = img.createGraphics(); 9 //clear the drawing area 10 graphics.setPaint(Color.white); 11 graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); 12 13 //render 14 slide[i].draw(graphics); 15 16 FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png"); 17 javax.imageio.ImageIO.write(img, "png", out); 18 out.close();
7)提取PPT文檔信息code
1 SlideShow ppt = new SlideShow(new HSLFSlideShow("PPT測試.ppt")); 2 Slide[] slides = ppt.getSlides(); 3 //提取文本信息 4 for (Slide each : slides) { 5 System.out.println(each.getTitle()) ; 6 TextRun[] textRuns = each.getTextRuns(); 7 for (int i=0 ;i< textRuns.length; i++ ) { 8 System.out.println(textRuns[i].getText()); 9 RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns(); 10 for (int j = 0; j < richTextRuns.length; j++) { 11 System.out.println(richTextRuns[j].getText()); 12 } 13 } 14 } 15 //提取全部JPEG圖片 16 PictureData[] picDatas = ppt.getPictureData(); 17 for (int i=0;i<picDatas.length;i++) { 18 if(picDatas[i].getType() == Picture.JPEG){ 19 FileOutputStream out = new FileOutputStream("jpg_" + i + ".jpg"); 20 ppt.write(out); 21 out.close(); 22 } 23 }
8)設置PPT文檔摘要信息(文檔點擊鼠標右鍵查看屬性)orm
1 HSLFSlideShow hslf = HSLFSlideShow.create(); 2 DocumentSummaryInformation dsi= hslf.getDocumentSummaryInformation(); 3 SummaryInformation si= hslf.getSummaryInformation(); 4 5 dsi.setCompany("yourCompany"); 6 dsi.setCategory("ppt測試"); 7 si.setAuthor("yourName"); 8 si.setTitle("標題"); 9 10 SlideShow ppt = new SlideShow(hslf); 11 savePPTFile(ppt);
1)若是是建立新的PPT文檔,直接使用SlideShow和Slide類就能夠,其中SlideShow表示PPT文檔,Slide表示某一張幻燈片
以下代碼建立空的PPT文檔:blog
2)設置母版,這樣後續的新建幻燈片都將使用母版的字體,背景等設置圖片
3)建立幻燈片並插入文本
4)插入圖片,支持多種格式
5)插入表格(v3.5)
6)若是是讀取已存在的PPT文檔則還要用到HSLFSlideShow,下面代碼將PPT文件導出爲圖片(png)格式,若是幻燈片上有中文字符則這些字符的字體須要修改成支持中文的字體(宋體等),不然導出的圖片的中文字符不能正常顯示
7)提取PPT文檔信息
8)設置PPT文檔摘要信息(文檔點擊鼠標右鍵查看屬性)