我是靠谱客的博主 结实小蝴蝶,这篇文章主要介绍java ppt转pdf,现在分享给大家,希望可以做个参考。

依赖


<!--
poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
<!-- itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>

代码工具


import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.hslf.usermodel.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;
import cn.hutool.core.util.StrUtil;
import com.itextpdf.text.Image;
public class PptToPdf {
public static void main(String[] args) {
pptx2Pdf("C:\Users\admin\Desktop\空白演示.pptx","E:\a\excel转pdf\1113.pdf");
}
public static boolean pptx2Pdf(String pptPath, String pdfPath) {
if (StrUtil.isEmpty(pptPath)) {
throw new RuntimeException("文档路径不能为空");
}
//
String pdfPath = pdfDir + "te." + "pdf";
Document document = null;
XMLSlideShow slideShow = null;
FileOutputStream fileOutputStream = null;
PdfWriter pdfWriter = null;
try {
//使用输入流pptx文件
slideShow = new XMLSlideShow(new FileInputStream(pptPath));
//获取幻灯片的尺寸
Dimension dimension = slideShow.getPageSize();
//新增pdf输出流,准备讲ppt写出
fileOutputStream = new FileOutputStream(pdfPath);
//创建一个写内容的容器
document = new Document();
//使用输出流写入
pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
//使用之前必须打开<You have to open the document before you can write content.>
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
//获取幻灯片
List<XSLFSlide> slideList = slideShow.getSlides();
for (int i = 0, row = slideList.size(); i < row; i++) {
//获取每一页幻灯片
XSLFSlide slide = slideList.get(i);
for (XSLFShape shape : slide.getShapes()) {
//判断是否是文本
if(shape instanceof XSLFTextShape){
// 设置字体, 解决中文乱码
XSLFTextShape textShape = (XSLFTextShape) shape;
for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
}
//根据幻灯片尺寸创建图形对象
BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
//把内容写入图形对象
slide.draw(graphics2d);
graphics2d.dispose();
//封装到Image对象中
Image image = Image.getInstance(bufferedImage, null);
image.scalePercent(50f);
// 写入单元格
pdfPTable.addCell(new PdfPCell(image, true));
document.add(image);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (document != null) {
document.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public static boolean pptToPdf(String pptPath, String pdfDir) {
if (StrUtil.isEmpty(pptPath)) {
throw new RuntimeException("word文档路径不能为空");
}
if (StrUtil.isEmpty(pdfDir)) {
throw new RuntimeException("pdf目录不能为空");
}
String pdfPath = pdfDir + "te." + "pdf";
Document document = null;
HSLFSlideShow hslfSlideShow = null;
FileOutputStream fileOutputStream = null;
PdfWriter pdfWriter = null;
try {
//使用输入流ppt文件
hslfSlideShow = new HSLFSlideShow(new FileInputStream(pptPath));
// 获取ppt文件页面
Dimension dimension = hslfSlideShow.getPageSize();
fileOutputStream = new FileOutputStream(pdfPath);
document = new Document();
// pdfWriter实例
pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();
for (int i=0; i < hslfSlideList.size(); i++) {
HSLFSlide hslfSlide = hslfSlideList.get(i);
for (HSLFShape shape : hslfSlide.getShapes()) {
if (shape instanceof HSLFTextShape){
// 设置字体, 解决中文乱码
HSLFTextShape textShape = (HSLFTextShape) shape;
for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
for (HSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
}
BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
hslfSlide.draw(graphics2d);
graphics2d.dispose();
Image image = Image.getInstance(bufferedImage, null);
image.scalePercent(50f);
// 写入单元格
pdfPTable.addCell(new PdfPCell(image, true));
document.add(image);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (document != null) {
document.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}

controller测试代码

	@PostMapping("/pptx2Pdf")
public String pptx2Pdf(MultipartFile file ) throws IllegalStateException, IOException{
String fileName = file.getOriginalFilename();
String localFilePath = StrUtil.appendIfMissing("/data/test", "/") + fileName;
file.transferTo(new File(localFilePath));
PptToPdf.pptx2Pdf(localFilePath, "/data/test/" + fileName.split("\.")[0] + ".pdf");
return "success可喜可贺";
}

最后

以上就是结实小蝴蝶最近收集整理的关于java ppt转pdf的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部