private static String windowsFontsForLinuxPath;
@Value("${linux字体文件路径}")
public void setNum(String windowsFontsForLinuxFolder){
windowsFontsForLinuxPath = windowsFontsForLinuxFolder;
}
private static List<XSLFShape> shapes = new ArrayList<>();
/**
* pptToPdf
* @param inputStream
* @param outputStream
* @return
*/
public static boolean pptToPdf(InputStream inputStream, OutputStream outputStream) {
Document document = null;
HSLFSlideShow hslfSlideShow = null;
PdfWriter pdfWriter = null;
try {
String os = System.getProperty("os.name"); //获取当前java运行
if(os != null && os.toLowerCase().startsWith("windows")){
}else if(os != null && os.toLowerCase().startsWith("linux")){ //当前环境:linux系统
//apose在linux服务器上生成pdf,内容乱码问题,解决代码:将windows的字体上传到linux,取linux上的字体列表
//FontSettings fontSettings = FontSettings.getDefaultInstance();
FontSettings.setFontsFolder(windowsFontsForLinuxPath + File.separator, true);
}
hslfSlideShow = new HSLFSlideShow(inputStream);
// 获取ppt文件页面
Dimension dimension = hslfSlideShow.getPageSize();
document = new Document();
// pdfWriter实例
pdfWriter = PdfWriter.getInstance(document, outputStream);
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();
for (HSLFSlide hslfSlide : hslfSlideList) {
// // 设置字体, 解决中文乱码
// 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("微软雅黑", FontGroup.EAST_ASIAN);
// textRun.setFontFamily("微软雅黑", FontGroup.LATIN);
// }
// }
// }
// }
BufferedImage bufferedImage = new BufferedImage((int) dimension.getWidth(), (int) dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
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 {
if (document != null) {
document.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
}
System.out.println("ppt转换完毕");
return true;
}
/**
* pptxToPdf
* @param inputStream
* @param outputStream
* @return
*/
public static boolean pptxToPdf(InputStream inputStream, OutputStream outputStream) {
Document document = null;
XMLSlideShow slideShow = null;
PdfWriter pdfWriter = null;
try {
String os = System.getProperty("os.name"); //获取当前java运行
if(os != null && os.toLowerCase().startsWith("windows")){
}else if(os != null && os.toLowerCase().startsWith("linux")){ //当前环境:linux系统
//apose在linux服务器上生成pdf,内容乱码问题,解决代码:将windows的字体上传到linux,取linux上的字体列表
//FontSettings fontSettings = FontSettings.getDefaultInstance();
FontSettings.setFontsFolder(windowsFontsForLinuxPath + File.separator, true);
}
slideShow = new XMLSlideShow(inputStream);
Dimension dimension = slideShow.getPageSize();
document = new Document();
pdfWriter = PdfWriter.getInstance(document, outputStream);
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
List<XSLFSlide> slideList = slideShow.getSlides();
for (XSLFSlide slide : slideList) {
for (XSLFShape shape : slide.getShapes()) {
//判断元素是否是组合元素,进入递归
if (shape instanceof XSLFGroupShape) {
handleGroupShape((XSLFGroupShape) shape);
}
//如果元素是文字,塞入待处理List
if (shape instanceof XSLFTextShape) {
setPPTXShape(shape);
}
}
//循环遍历得到的该页pptx上的所有文字元素,全部统一set为宋体
for (XSLFShape xslfShape : shapes){
XSLFTextShape textShape = (XSLFTextShape) xslfShape;
for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
BufferedImage bufferedImage = new BufferedImage(dimension.width,dimension.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(new Font("微软雅黑", Font.PLAIN, 15));
graphics2d.fill(new Rectangle2D.Float(0, 0, dimension.width, dimension.height));
//字体抗锯齿不失真
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
slide.draw(graphics2d);
graphics2d.dispose();
shapes.clear();
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 {
if (document != null) {
document.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
shapes.clear();
}
System.out.println("pptx转换完毕");
return true;
}
//将单个元素放入待处理List
private static void setPPTXShape(XSLFShape xslfShape){
shapes.add(xslfShape);
}
// 处理 XSLFGroupShape 的方法
private static void handleGroupShape(XSLFGroupShape groupShape) {
List<XSLFShape> shapes = groupShape.getShapes();
for (XSLFShape shapeChild : shapes) {
// 递归处理子形状
if (shapeChild instanceof XSLFGroupShape) {
handleGroupShape((XSLFGroupShape) shapeChild);
} else if (shapeChild instanceof XSLFTextShape) {
setPPTXShape(shapeChild);
}
}
}
ppt、pptx转换为pdf
于 2023-11-27 09:04:08 首次发布