Java 利用POI操作PPT


解析PPT文件中的圖片

import java.io.File;    
    import java.io.FileOutputStream;    

    import org.apache.poi.hslf.HSLFSlideShow;    
    import org.apache.poi.hslf.model.Picture;    
    import org.apache.poi.hslf.usermodel.PictureData;    
    import org.apache.poi.hslf.usermodel.SlideShow;    

   public class OutputPicture {    
       // 圖片默認存放路徑    
       public final static String path = "F:\\ppt\";    

       public static void main(String[] args) throws Exception {    
           // 加載PPT    
           HSLFSlideShow _hslf = new HSLFSlideShow("F:\\Downloads\\myPPT.ppt");    
           SlideShow _slideShow = new SlideShow(_hslf);    

           // 獲取PPT文件中的圖片數據    
           PictureData[] _pictures = _slideShow.getPictureData();    

           // 循環讀取圖片數據    
           for (int i = 0; i < _pictures.length; i++) {    
               StringBuilder fileName = new StringBuilder(path);    
               PictureData pic_data = _pictures[i];    
               fileName.append(i);    
               // 設置格式    
               switch (pic_data.getType()) {    
             case Picture.JPEG:    
                   fileName.append(".jpg");    
                   break;    
               case Picture.PNG:    
                   fileName.append(".png");    
                   break;    
               default:    
                   fileName.append(".data");    
               }    
               // 輸出文件    
               FileOutputStream fileOut = new FileOutputStream(new File(fileName.toString()));    
               fileOut.write(pic_data.getData());    
               fileOut.close();    
           }    
       }    
   }   

在PPT文件中加入外部圖片

import java.awt.Rectangle;    
   import java.awt.image.BufferedImage;    
   import java.io.File;    
   import java.io.FileOutputStream;    
   import javax.imageio.ImageIO;    
   import org.apache.poi.hslf.model.Picture;    
   import org.apache.poi.hslf.model.Slide;    
   import org.apache.poi.hslf.usermodel.SlideShow;    


   public class InputPicture {    

       public static String path = "F:\\images\\myImage.png";    
       public static String OUTPUT = "F:\\ppt\\myppt.ppt";    

       public static void main(String[] args) throws Exception {    

           if(args.length != 0){    
               path = args[0];    
           }    
           // 構建PPT    
           SlideShow _slideShow = new SlideShow();    
           // 創建幻燈片    
           Slide _slide = _slideShow.createSlide();    

           // 設置圖片類型    
           int pic_type = -1;    
           if(path.indexOf(".png") != -1){    
               pic_type = Picture.PNG;    
           }else{    
               pic_type = Picture.JPEG;    
           }    
           File file = new File(path);    
           BufferedImage image = ImageIO.read(file);    
           // 新置入圖片索引位置    
          int newIndex = _slideShow.addPicture(file, pic_type);    
           // 根據索引位置 , 創建圖片對象    
          Picture _picture = new Picture(newIndex);    
           // 設置圖片顯示位置    
           _picture.setAnchor(new Rectangle(100,100,image.getWidth(),image.getHeight()));    

           // 將圖片放入幻燈片    
           _slide.addShape(_picture);    
           // 輸出PPT文件    
           _slideShow.write(new FileOutputStream(new File(OUTPUT)));    
       }    

   }  

操作文本對象

import java.awt.Color;    
         import java.awt.Rectangle;    
         import java.io.FileOutputStream;    

         import org.apache.poi.hslf.model.AutoShape;    
         import org.apache.poi.hslf.model.Line;    
         import org.apache.poi.hslf.model.ShapeTypes;    
         import org.apache.poi.hslf.model.Slide;    
         import org.apache.poi.hslf.model.TextBox;    
         import org.apache.poi.hslf.model.TextRun;    
         import org.apache.poi.hslf.usermodel.RichTextRun;    
         import org.apache.poi.hslf.usermodel.SlideShow;    


         public class InputTextRun {    

             public static void main(String[] args) throws Exception{    

                 SlideShow _slideShow = new SlideShow();    
                 Slide slide = _slideShow.createSlide();    

                 // 創建並置入簡單文本    
                 TextBox _text = new TextBox();    
                 TextRun _textRun = _text.createTextRun();    
                 _textRun.setRawText("杜磊米");    
                 _text.setAnchor(new Rectangle(10,10,100,100));    

                 // 創建並置入帶有樣式的文本    
                 AutoShape _autoShape = new AutoShape(ShapeTypes.Rectangle); //設置形狀    
                TextRun _autoText = _autoShape.createTextRun();    
                 _autoText.setRawText("杜磊米");    
                 _autoShape.setAnchor(new Rectangle(200,200,100,100));    
                 _autoShape.setFillColor(new Color(170,215,255));    
                 _autoShape.setLineWidth(5.0);    
                 _autoShape.setLineStyle(Line.LINE_DOUBLE);    

                 // AutoShape 對象可以設置多個不同樣式文本    
                 TextRun _autoText2 = _autoShape.createTextRun();    
                 RichTextRun _richText = _autoText2.appendText("杜");    
                 _richText.setFontColor(new Color(255,255,255));    
                 RichTextRun _richText2 = _autoText2.appendText("磊米");    
                 _richText2.setFontColor(new Color(255,0,0));    
                 _richText2.setFontSize(12);    

                 // 將文本對象置入幻燈片    
                 slide.addShape(_text);    
                 slide.addShape(_autoShape);    

                 // 輸出文件    
                 _slideShow.write(new FileOutputStream("F:\\ppt\\text.ppt"));    

             }    

         }  

設置各類文件屬性

 import java.awt.Dimension;    
   import java.io.FileOutputStream;    

   import org.apache.poi.hpsf.DocumentSummaryInformation;    
   import org.apache.poi.hpsf.SummaryInformation;    
   import org.apache.poi.hslf.HSLFSlideShow;    
   import org.apache.poi.hslf.model.Slide;    
   import org.apache.poi.hslf.usermodel.SlideShow;    


   public class PPTProperty {    
       public static void main(String [] args)throws Exception{    

           HSLFSlideShow hslf = HSLFSlideShow.create();    
           SlideShow _slideShow = new SlideShow(hslf);    
           // 設置頁面大小    
           _slideShow.setPageSize(new Dimension(400,600));    
           // 設置后創建出相應大小的幻燈片    
           Slide slide = _slideShow.createSlide();    

           DocumentSummaryInformation doc = hslf.getDocumentSummaryInformation();    
           SummaryInformation info = hslf.getSummaryInformation();    

           doc.setCompany("secret");    
           info.setAuthor("杜磊米");    
           info.setTitle("nothing");    
           // 輸出文件    
           _slideShow.write(new FileOutputStream("F:\\ppt\\demo.ppt"));    

           // 完成后, 找到文件 , 右鍵屬性查看:)    
       }    
   }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM