-
2022-03-14 15:29:28
pom文件添加
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.12</version> </dependency>
ppt转图片工具类
public class PPTtoImageUtils { private static final Logger logger = LoggerFactory.getLogger(PPTtoImageUtils.class); //支持pptx public static List<String> pptxToImage(MultipartFile file, String bucket) { String originalFileName = file.getOriginalFilename(); String rootPath = bucket + "/" + System.currentTimeMillis(); List<String> urlList = Lists.newArrayList(); try { InputStream is = file.getInputStream(); XMLSlideShow ppt = new XMLSlideShow(is); is.close(); Dimension pgsize = ppt.getPageSize(); logger.info("pptx to image file [name:{}] [width:{}] [height:{}]", originalFileName, pgsize.width, pgsize.height); for (int i = 0; i < ppt.getSlides().size(); i++) { //防止中文乱码 for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) { if (shape instanceof XSLFTextShape) { XSLFTextShape tsh = (XSLFTextShape) shape; for (XSLFTextParagraph p : tsh) { for (XSLFTextRun r : p) { r.setFontFamily("宋体"); } } } } BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); // clear the drawing area graphics.setPaint(Color.white); graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); // render ppt.getSlides().get(i).draw(graphics); // save the output String filename = (i + 1) + ".png"; // 创建输出流 ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 将图像输出到输出流中。 ImageIO.write(img, "png", bos); MultipartFile multipartFile = new MockMultipartFile(filename, filename, "image/png", bos.toByteArray()); //上传到oss String url = UploadUtils.simpleUpload(multipartFile, rootPath); urlList.add(url); } } catch (Exception e) { logger.error("pptx to image error [name:{}][e:{}] ", originalFileName,e); throw new BusinessException(ErrorCode.PPT_TO_IMAGE_ERROR); } logger.info("pptx to image success [name:{}] ", originalFileName); return urlList; } //支持ppt public static List<String> pptToImage(MultipartFile file, String bucket) { String originalFileName = file.getOriginalFilename(); String rootPath = bucket + "/" + System.currentTimeMillis(); List<String> urlList = Lists.newArrayList(); try { InputStream is = file.getInputStream(); HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(is)); Dimension pgsize = ppt.getPageSize(); for (int i = 0; i < ppt.getSlides().size(); i++) { //防止中文乱码 for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) { if (shape instanceof HSLFTextShape) { HSLFTextShape tsh = (HSLFTextShape) shape; for (HSLFTextParagraph p : tsh) { for (HSLFTextRun r : p) { r.setFontFamily("宋体"); } } } } BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); // clear the drawing area graphics.setPaint(Color.white); graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); // render ppt.getSlides().get(i).draw(graphics); String filename = (i + 1) + ".png"; // 创建输出流 ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 将图像输出到输出流中。 ImageIO.write(img, "png", bos); MultipartFile multipartFile = new MockMultipartFile(filename, filename, "image/png", bos.toByteArray()); //上传到oss String url = UploadUtils.simpleUpload(multipartFile, rootPath); urlList.add(url); } logger.info("ppt to image success [name:{}] ", originalFileName); return urlList; } catch (Exception e) { logger.error("ppt to image error [name:{}] [e:{}]", originalFileName,e); throw new BusinessException(ErrorCode.PPT_TO_IMAGE_ERROR); } }
更多相关内容 -
Apache POI PPT - 演示( Presentation)
2021-04-22 06:37:37Apache POI PPT - 演示( Presentation)通常,我们使用MS-PowerPoint来创建演示文稿。 现在让我们看看如何使用Java创建演示文稿。 完成本章后,您将能够使用Java程序创建新的MS-PowerPoint演示文稿并打开现有的PPT。...Apache POI PPT - 演示( Presentation)
通常,我们使用MS-PowerPoint来创建演示文稿。 现在让我们看看如何使用Java创建演示文稿。 完成本章后,您将能够使用Java程序创建新的MS-PowerPoint演示文稿并打开现有的PPT。
创建空演示文稿
要创建一个空的表示,您必须实例化org.poi.xslf.usermodel包的XMLSlideShow类 -XMLSlideShow ppt = new XMLSlideShow();
使用FileOutputStream类将更改保存到PPT文档 -File file = new File("C://POIPPT//Examples//example1.pptx");
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);
以下是创建空白MS-PowerPoint演示文稿的完整程序。import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class CreatePresentation {
public static void main(String args[]) throws IOException {
//creating a new empty slide show
XMLSlideShow ppt = new XMLSlideShow();
//creating an FileOutputStream object
File file = new File("example1.pptx");
FileOutputStream out = new FileOutputStream(file);
//saving the changes to a file
ppt.write(out);
System.out.println("Presentation created successfully");
out.close()
}
}
将上述Java代码保存为CreatePresentation.java ,然后从命令提示符编译并执行它,如下所示 -$javac CreatePresentation.java
$java CreatePresentation
如果系统环境配置了POI库,它将编译并执行以在当前目录中生成名为example1.pptx的空白PPT文件,并在命令提示符下显示以下输出 -Presentation created successfully
空白PowerPoint文档如下所示 -
编辑现有演示文稿
要打开现有演示文稿,请实例化XMLSlideShow类并将要编辑的文件的FileInputStream对象作为XMLSlideShow构造函数的参数传递。File file = new File(“C://POIPPT//Examples//example1.pptx”);
FileInputstream inputstream = new FileInputStream(file);
XMLSlideShow ppt = new XMLSlideShow(inputstream);
您可以使用org.poi.xslf.usermodel包中的XMLSlideShow类的createSlide()方法将幻灯片添加到演示文稿中。XSLFSlide slide1 = ppt.createSlide();
以下是打开幻灯片并将幻灯片添加到现有PPT的完整程序 -import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class EditPresentation {
public static void main(String ar[]) throws IOException {
//opening an existing slide show
File file = new File("example1.pptx");
FileInputStream inputstream = new FileInputStream(file);
XMLSlideShow ppt = new XMLSlideShow(inputstream);
//adding slides to the slodeshow
XSLFSlide slide1 = ppt.createSlide();
XSLFSlide slide2 = ppt.createSlide();
//saving the changes
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);
System.out.println("Presentation edited successfully");
out.close();
}
}
将上述Java代码保存为EditPresentation.java ,然后从命令提示符编译并执行它,如下所示 -$javac EditPresentation.java
$java EditPresentation
它将编译并执行以生成以下输出 -slides successfully added
带有新添加幻灯片的输出PPT文档如下所示 -
将幻灯片添加到PPT后,您可以在幻灯片上添加,执行,读取和写入操作。
Apache POI PPT - 类和方法( Classes & Methods)
-
POI PPT模板字段替换
2019-11-15 12:53:00#遇到一个需求就是,给一个固定模板往固定位置填写代码 <dependency> <!-- LEVEL-PLT:操作EXCEL...org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.13</v...#遇到一个需求就是,给一个固定模板往固定位置填写代码
<dependency> <!-- LEVEL-PLT:操作EXCEL所需要的包 --> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.13</version> </dependency> <dependency> <!-- LEVEL-PLT:提供对office的word,excel,visio,ppt的操作 --> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.13</version> </dependency>
图片的替换字段要用插入文本框插入,而且不能复制,只能一个一个插入
#代码package com.baic.util; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.xslf.usermodel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ebon.framework.util.NullUtil; import java.awt.*; import java.awt.geom.Rectangle2D; import java.io.*; import java.util.List; import java.util.Map; /** * 读取ppt模板替换模板指定数据 * * @author wuyahui * */ public class PPTToNewPPTUtils { private static Logger logger = LoggerFactory.getLogger(PPTToNewPPTUtils.class); public static void changPPT(String inputUrl,String outputUrl, Map<String, String> textMap) { try { //读取模板ppt SlideShow ppt = new XMLSlideShow(new FileInputStream(new File(inputUrl))); //提取文本信息 List<XSLFSlide> slides = ppt.getSlides(); for (XSLFSlide slide : slides) { List<XSLFShape> shapes = slide.getShapes(); for(int i=0;i<shapes.size();i++){ Rectangle2D anchor = shapes.get(i).getAnchor(); if (shapes.get(i) instanceof XSLFTextBox) { XSLFTextBox txShape = (XSLFTextBox) shapes.get(i); if (txShape.getText().contains("{rName}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("rName"))) { txShape.setText(txShape.getText().replace("{rName}", textMap.get("rName"))); } else { txShape.setText(txShape.getText().replace("{rName}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(10d); // 设置字体大小 } if (txShape.getText().contains("{pName}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("pName"))) { txShape.setText(txShape.getText().replace("{pName}", textMap.get("pName"))); } else { txShape.setText(txShape.getText().replace("{pName}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(10d); // 设置字体大小 } if (txShape.getText().contains("{code}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("code"))) { txShape.setText(txShape.getText().replace("{code}", textMap.get("code"))); } else { txShape.setText(txShape.getText().replace("{code}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(10d); // 设置字体大小 } if (txShape.getText().contains("{tCode}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("tCode"))) { txShape.setText(txShape.getText().replace("{tCode}", textMap.get("tCode"))); } else { txShape.setText(txShape.getText().replace("{tCode}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(10d); // 设置字体大小 } if (txShape.getText().contains("{version}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("version"))) { txShape.setText(txShape.getText().replace("{version}", textMap.get("version"))); } else { txShape.setText(txShape.getText().replace("{version}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(10d); // 设置字体大小 } if (txShape.getText().contains("{name}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name"))) { txShape.setText(txShape.getText().replace("{name}", textMap.get("name"))); } else { txShape.setText(txShape.getText().replace("{name}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(30d); // 设置字体大小 txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontFamily("微软雅黑 (标题)"); // 字体格式 txShape.getTextParagraphs().get(0).getTextRuns().get(0).setBold(true); // 加粗 txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontColor(new Color(0,151,224)); // 字体颜色 txShape.setHorizontalCentered(true); // 置中 } if (txShape.getText().contains("{name1}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name1"))) { txShape.setText(txShape.getText().replace("{name1}", textMap.get("name1"))); } else { txShape.setText(txShape.getText().replace("{name1}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } if (txShape.getText().contains("{name2}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name2"))) { txShape.setText(txShape.getText().replace("{name2}", textMap.get("name2"))); } else { txShape.setText(txShape.getText().replace("{name2}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } if (txShape.getText().contains("{name3}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name3"))) { txShape.setText(txShape.getText().replace("{name3}", textMap.get("name3"))); } else { txShape.setText(txShape.getText().replace("{name3}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } if (txShape.getText().contains("{name4}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name4"))) { txShape.setText(txShape.getText().replace("{name4}", textMap.get("name4"))); } else { txShape.setText(txShape.getText().replace("{name4}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } if (txShape.getText().contains("{name5}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name5"))) { txShape.setText(txShape.getText().replace("{name5}", textMap.get("name5"))); } else { txShape.setText(txShape.getText().replace("{name5}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } if (txShape.getText().contains("{name6}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name6"))) { txShape.setText(txShape.getText().replace("{name6}", textMap.get("name6"))); } else { txShape.setText(txShape.getText().replace("{name6}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } if (txShape.getText().contains("{name7}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("name7"))) { txShape.setText(txShape.getText().replace("{name7}", textMap.get("name7"))); } else { txShape.setText(txShape.getText().replace("{name7}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } if (txShape.getText().contains("{date}")) { // 替换文字内容.用TextRun获取替换的文本来设置样式 if (NullUtil.isNotNull(textMap.get("date"))) { txShape.setText(txShape.getText().replace("{date}", textMap.get("date"))); } else { txShape.setText(txShape.getText().replace("{date}", "")); } txShape.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(12d); // 设置字体大小 } } } } FileOutputStream outputStreams = new FileOutputStream(new File(outputUrl)); ppt.write(outputStreams); outputStreams.close(); }catch (IOException e) { logger.error("交付物ppt模板封面生成", e); } } }
-
Apache POI将PPT转换成图片实例代码
2020-08-28 06:46:18主要介绍了Apache POI将PPT转换成图片实例代码,具有一定借鉴价值,需要的朋友可以参考下 -
Apache POI PPT - 合并( Merging)
2021-03-17 11:43:30Apache POI PPT - 合并( Merging)您可以使用XMLSlideShow类的importContent()方法合并多个演示文稿。 以下是合并两个演示文稿的完整程序 -import java.io.FileInputStream;import java.io.FileOutputStream;import ...Apache POI PPT - 合并( Merging)
您可以使用XMLSlideShow类的importContent()方法合并多个演示文稿。 以下是合并两个演示文稿的完整程序 -import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class MergingMultiplePresentations {
public static void main(String args[]) throws IOException {
//creating empty presentation
XMLSlideShow ppt = new XMLSlideShow();
//taking the two presentations that are to be merged
String file1 = "presentation1.pptx";
String file2 = "presentation2.pptx";
String[] inputs = {file1, file2};
for(String arg : inputs){
FileInputStream inputstream = new FileInputStream(arg);
XMLSlideShow src = new XMLSlideShow(inputstream);
for(XSLFSlide srcSlide : src.getSlides()) {
//merging the contents
ppt.createSlide().importContent(srcSlide);
}
}
String file3 = "combinedpresentation.pptx";
//creating the file object
FileOutputStream out = new FileOutputStream(file3);
// saving the changes to a file
ppt.write(out);
System.out.println("Merging done successfully");
out.close();
}
}
将上面的代码保存为MergingMultiplePresentations.java ,然后从命令提示符编译并执行它,如下所示 -$javac MergingMultiplePresentations.java
$java MergingMultiplePresentations
它将编译并执行以生成以下输出 -Merging done successfully
以下快照显示了第一个演示文稿 -
以下快照显示第二个演示文稿 -
下面给出了合并两张幻灯片后程序的输出。 在这里,您可以看到合并在一起的早期幻灯片的内容。
Apache POI PPT - 格式化文本( Formatting Text)
-
Apache POI PPT - 幻灯片布局( Slide Layouts)
2021-03-22 12:20:50Apache POI PPT - 幻灯片布局( Slide Layouts)在上一章中,您已经了解了如何创建空幻灯片以及如何向其添加幻灯片。 在本章中,您将学习如何获取可用幻灯片列表,以及如何创建具有不同布局的幻灯片。可用的幻灯片布局... -
poi操作ppt生成图表完整工程.zip
2017-05-12 23:13:28poi操作ppt生成图表完整工程 -
java实现poi模板生成PPT文件代码
2019-12-26 00:17:57java实现poi模板生成PPT文件代码:两个迭代版本。java实现poi模板生成PPT文件代码:两个迭代版本。 -
poi ppt word
2017-10-31 17:04:17pot ppt word * 如涉及侵权内容,您的资源将被移除 * 请勿上传小说、mp3、图片等与技术无关的内容.一旦发现将被删除 * 请勿在未经授权的情况下上传任何涉及著作权侵权的资源,除非该资源完全由您个人创作 * 点击上传... -
poi操作ppt图表史上最完整示例演示.zip
2017-05-13 17:39:15poi操作ppt图表史上最完整示例演示.zip 示例包含圆饼图、柱状图、线性图、面积图 -
Apache POI PPT - 幻灯片布局
2021-02-25 18:34:51slide created successfully 带有新添加的标题布局幻灯片的PPT文档如下所示: 标题和内容布局 让我们使用标题和内容布局在PPT中创建幻灯片。 按照下面给出的步骤。 步骤1:通过实例化XMLSlideShow类创建一个空的演示... -
Apache POI PPT - 类和方法( Classes & Methods)
2021-02-28 11:47:38Apache POI PPT - 类和方法( Classes & Methods)在本章中,我们将了解Apache POI API下的一些类和方法,这些类和方法对于使用Java程序处理PPT文件至关重要。演讲(Presentation)要创建和管理演示文稿,请在包org.... -
POI操作PPT文档(导入,导出,读取,添加,拼接,替换文本,页面排序)
2018-07-06 19:00:24POI操作PPT文档(导入,导出,读取,添加,拼接,替换文本,页面排序) -
poi ppt转换为html,实现在线预览
2021-03-06 17:13:02ppt转换为html的原理就是将ppt转换为图片import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics2D;import java.awt.Image;import java.awt.geom.Rectangle2D;import java.awt.image.... -
java poi ppt 接口的基本操作
2021-03-14 18:24:18依赖在 pom.xml中增加以下依赖org.apache.poipoi-ooxml4.1.1注:很多博客,教我们用以下依赖,是没有XSSF相关内容的org.apache.poipoi3.14version 版本找到想要依赖的版本点击进入后,可以直接复制里面的依赖初始化... -
java用poi转ppt为图片和用pdfbox转pdf为图片的demo
2018-08-29 19:56:39java用poi转ppt为图片和用pdfbox转pdf为图片的demo。里面包含两块具体的demo和多个函数 -
poi ppt转图片字体乱码
2017-02-07 08:18:46调用poi,将ppt/pptx文件转化成图片序列,设置默认字体为微软雅黑,但还是有部分字体出现乱码 server发布在linux上。 -
POI-3.14处理ppt和pptx
2016-04-26 15:07:11新版POI-3.14解决poi之前版本ppt读取不完整bug -
poi操作ppt完整示例程序
2019-12-26 01:16:26poi操作ppt完整示例程序,包括java实现的demo+POI-3.15的所有jar文件。poi操作ppt完整示例程序,包括java实现的demo+POI-3.15的所有jar文件。 -
使用POI读写PowerPoint文件(兼容ppt与pptx版本)
2019-04-26 01:17:47NULL 博文链接:https://chong0660.iteye.com/blog/1923760 -
通过poi读取ppt元素demo
2021-09-17 15:39:04poi+Spire.Presentation for Java获取导入ppt元素 最近有一个导入ppt,识别ppt内所有元素需求,翻了一些资料都没有特别好的demo,官方文档很官方...,所以打算自己写一个demo。 poi 4.1,因为项目里之前引入的就是4.1... -
PPT标签水印
2017-03-29 09:42:58通过poi修改添加ppt图片、文字水印 -
Apache POI PPT - 合并
2021-03-17 11:43:58您可以使用XMLSlideShow类的importContent()方法合并多个演示文稿。 下面给出的是合并两个演示文稿的完整程序:import java.io.FileInputStream;...import org.apache.poi.xslf.usermodel.XMLSlid... -
利用Apache POI操作ppt模板
2021-01-15 15:25:20利用Apache POI操作ppt模板 实现的是读取ppt模板,修改动态数据,输出新ppt文件 构建的时maven项目所依赖如下 <dependencies> <dependency> <groupId>org.springframework.boot</groupId&... -
Poi PPT PPTX 处理 [生产环境使用]
2020-12-21 13:36:53519718366115 天前http://poi.apache.org/slideshow/how-to-shapes.html#RenderHSLF provides a way to export slides into images. You can capture slides into java.awt.Graphics2D object (or any other) and ... -
通过POI将PPT插入图片并导出实例
2013-07-13 16:57:52通过POI生成插入图片的PPT。 包括图片的自动生成。 -
java使用poi读取ppt文件和poi读取excel、word示例
2020-09-04 13:39:06主要介绍了java使用poi读取ppt文件和poi读取excel、word示例,需要的朋友可以参考下 -
Apache POI操作PPT: 文字替换 图片替换 表格填充 PPT合并
2020-07-05 10:44:15工作中经常使用的一些PPT操作。