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