2017-08-21 60 views
0

我想使用apache poi hslf在powerpoint中添加一個文本框。在文本框中,我想添加一個標題,然後在同一個文本框中添加項目符號。但是,如果我應用richtextrun.setbullet(true);它也將標題放在標題中,儘管標題是在單獨的富文本文件中運行。任何幫助,將不勝感激。我附上示例代碼。使用apache poi hslf在ppt中添加子彈和非子彈

import org.apache.poi.hslf.record.StyleTextPropAtom; 
import org.apache.poi.hslf.record.TextCharsAtom; 
import org.apache.poi.hslf.record.Record; 
import org.apache.poi.hslf.model.*; 
import org.apache.poi.hslf.model.textproperties.TextPropCollection; 
import org.apache.poi.hslf.usermodel.SlideShow; 
import org.apache.poi.hslf.usermodel.RichTextRun; 

import java.awt.*; 
import java.io.*; 

public class test { 

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

     SlideShow ppt = new SlideShow(); 
     Slide slide = ppt.createSlide(); 
     TextShape shape = new TextBox(); 
     shape.setSheet(slide); 

     TextRun run = shape.getTextRun(); 
     StyleTextPropAtom styleAtom = null; 
     TextCharsAtom textAtom = null; 
     for(Record r : run.getRecords()){ 
      if(r instanceof StyleTextPropAtom) styleAtom = (StyleTextPropAtom)r; 
      else if(r instanceof TextCharsAtom) textAtom = (TextCharsAtom)r; 
     } 

     styleAtom.getParagraphStyles().clear(); 
     styleAtom.getCharacterStyles().clear(); 

     StringBuffer text = new StringBuffer(); 
     TextPropCollection prProps, chProps; 
     RichTextRun rt; 
     String chunk; 

     //begin building rich text runs 

     //this should be heading and without bullet ppoint 
     chunk = " Apache POI"; 

     text.append(chunk); 
     prProps = styleAtom.addParagraphTextPropCollection(chunk.length()); 
     chProps = styleAtom.addCharacterTextPropCollection(chunk.length()); 
     rt = new RichTextRun(shape.getTextRun(), text.length(), chunk.length(), null, chProps, false, false); 
     rt.supplySlideShow(ppt); 
     rt.setFontColor(Color.green); 
     rt.setItalic(true); 
     rt.setFontSize(24); 

     String chunk = " \r is \r cool"; 
     int len = chunk.length(); 
     text.append(chunk); 
     prProps = styleAtom.addParagraphTextPropCollection(chunk.length()); 
     chProps = styleAtom.addCharacterTextPropCollection(chunk.length()); 
     rt = new RichTextRun(shape.getTextRun(), text.length(), chunk.length(), prProps, chProps, false, false); 
     rt.supplySlideShow(ppt); 
     PPFont font = new PPFont(); 
     font.setFontName("Times New Roman"); 
     int fontIndex = ppt.addFont(font); 
     rt.setFontIndex(fontIndex); 
     rt.setBold(true); 
     rt.setFontSize(24); 
     rt.setBullet(true); 

     //sum of chunk lengths must be text.length+1, add a dummy char to the end 
     styleAtom.addParagraphTextPropCollection(1); 
     styleAtom.addCharacterTextPropCollection(1); 

     String txt = text.toString(); 
     textAtom.setText(txt); 
     shape.getTextRun().buildRichTextRuns(styleAtom.getParagraphStyles(), styleAtom.getCharacterStyles(), txt); 
     //end building rich text runs 

     shape.setAnchor(new Rectangle(100, 100, 300, 50)); 
     slide.addShape(shape); 

     FileOutputStream out = new FileOutputStream("test.ppt"); 
     ppt.write(out); 
     out.close(); 

    } 

} 

由於提前

電流輸出 enter image description here

我不想讓子彈一線

回答

1

應當更新POI罐子,然後將下面的工作:

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

import org.apache.poi.hslf.usermodel.HSLFSlide; 
import org.apache.poi.hslf.usermodel.HSLFSlideShow; 
import org.apache.poi.hslf.usermodel.HSLFTextBox; 
import org.apache.poi.hslf.usermodel.HSLFTextRun; 

public class BulletTest { 
    public static void main(String[] args) throws IOException { 
     HSLFSlideShow ppt = new HSLFSlideShow(); 
     HSLFSlide slide = ppt.createSlide(); 
     HSLFTextBox tb = slide.createTextBox(); 
     tb.setAnchor(new Rectangle(100, 100, 200, 200)); 

     HSLFTextRun titleTR = tb.appendText("Title", true); 
     HSLFTextRun bullet1TR = tb.appendText(" bullet1", true); 
     bullet1TR.getTextParagraph().setBullet(true); 
     HSLFTextRun bullet2TR = tb.appendText(" bullet2", true); 
     bullet2TR.getTextParagraph().setBullet(true); 

     FileOutputStream fos = new FileOutputStream("bullet.ppt"); 
     ppt.write(fos); 
     fos.close(); 
     ppt.close(); 
    } 
} 
+0

謝謝一堆:)它正在按預期工作d :) – user3768904