首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ppt中的apache poi hslf添加项目符号和非项目符号

使用ppt中的apache poi hslf添加项目符号和非项目符号
EN

Stack Overflow用户
提问于 2017-08-22 03:35:25
回答 1查看 772关注 0票数 1

我想在powerpoint中使用apache poi hslf添加一个文本框。在文本框中,我想添加一个标题,然后在相同的文本框中添加项目符号。但是如果我应用richtextrun.setbullet(True),它也会将项目符号和标题放在一起,尽管标题在单独的富文本运行中。任何帮助都将不胜感激。我附上了示例代码。

代码语言:javascript
复制
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(); 

     } 

} 

提前感谢

当前输出为

我不想要第一行的子弹

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-22 07:39:27

您应该更新POI jars,然后执行以下操作:

代码语言:javascript
复制
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();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45804388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档