用poi的XSLF创建ppt,添加文本的时候多了空行
2016-06-20 14:49
239 查看
poi版本3.14.
根据poi的demo写了个简单的生成ppt的例子:
使用如下代码看看到底生成了什么东西:
发现shape1的那个XSLFTextBox其实有两个XSLFTextParagraph对象。
查看相应的代码,问题应该是出在XSLFTextParagraph p1 = shape1.addNewTextParagraph();这一句。原因应该是XSLFTextBox本身就有一个TextParagraph了。
这一行代码改成:
就可以了。
根据poi的demo写了个简单的生成ppt的例子:
public static void makePpt(String path) throws Exception { if (path == null) { path = "e:/text.pptx"; } XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide1 = ppt.createSlide(); XSLFTextBox shape1 = slide1.createTextBox(); Rectangle anchor = new Rectangle(10, 100, 300, 100); shape1.setAnchor(anchor); XSLFTextParagraph p1 = shape1.addNewTextParagraph(); XSLFTextRun r1 = p1.addNewTextRun(); r1.setFontColor(new Color(0, 200, 160)); shape1.setFillColor(Color.red); r1.setText("text1"); XSLFTextBox shape2 = slide1.createTextBox(); Rectangle anchor2 = new Rectangle(210, 200, 300, 100); shape2.setAnchor(anchor2); shape2.setText("text2"); FileOutputStream out = new FileOutputStream(path); ppt.write(out); out.close(); ppt.close(); }发现text1的文本框多了一个空行。text2文本框没有多空行。
使用如下代码看看到底生成了什么东西:
public static void readPpt() throws Exception { XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("e:/text.pptx")); XSLFSlide slide1 = ppt.getSlides().get(0); for (XSLFShape shape : slide1.getShapes()) { if (shape instanceof XSLFTextBox) { XSLFTextBox box = (XSLFTextBox) shape; java.util.List<XSLFTextParagraph> ps = box.getTextParagraphs(); if (ps.size() > 0) { java.util.List<XSLFTextRun> rs = ps.get(0).getTextRuns(); if (!rs.isEmpty()) { System.out.println(rs.get(0).getRawText()); } } } } }
发现shape1的那个XSLFTextBox其实有两个XSLFTextParagraph对象。
查看相应的代码,问题应该是出在XSLFTextParagraph p1 = shape1.addNewTextParagraph();这一句。原因应该是XSLFTextBox本身就有一个TextParagraph了。
这一行代码改成:
XSLFTextParagraph p1 =shape1.getTextParagraphs().isEmpty() ? shape1.addNewTextParagraph() : shape1.getTextParagraphs().get(0);
就可以了。
相关文章推荐
- 如何使用 Beamer 创建演示文稿
- ASP.NET中实现导出ppt文件数据的实例分享
- C#实现将PPT转换成HTML的方法
- 使用impress.js制作幻灯片
- java读取word-excel-ppt文件代码
- PHP读取PPT文件的方法
- Android SDK 百度地图通过poi城市内检索简介接口的使用
- Android实现带列表的地图POI周边搜索功能
- Android百度地图poi范围搜索
- Android 百度地图POI搜索功能实例代码
- Java使用poi操作excel实例解析
- Java 使用poi把数据库中数据导入Excel的解决方法
- java使用POI读取properties文件并写到Excel的方法
- java使用poi读取ppt文件和poi读取excel、word示例
- java实现PPT转PDF出现中文乱码问题的解决方法
- Python实现对PPT文件进行截图操作的方法
- Python输出PowerPoint(ppt)文件中全部文字信息的方法
- 如何写好PPT