标签:
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文档:
2)设置母版,这样后续的新建幻灯片都将使用母版的字体,背景等设置
3)创建幻灯片并插入文本
4)插入图片,支持多种格式
5)插入表格(v3.5)
6)如果是读取已存在的PPT文档则还要用到HSLFSlideShow,下面代码将PPT文件导出为图片(png)格式,如果幻灯片上有中文字符则这些字符的字体需要修改为支持中文的字体(宋体等),否则导出的图片的中文字符不能正常显示
7)提取PPT文档信息
8)设置PPT文档摘要信息(文档点击鼠标右键查看属性)
标签:
原文地址:http://www.cnblogs.com/kingxiaozi/p/4599384.html