ppt2PDF

ppt 转 pdf

使用poiitext库实现,兼容 ppt 和 pptx。

注: 本人在使用中遇到,组合图形中的中文乱码。

maven 依赖

itext 依赖了 poi 不用手动添加。 poi 中ppt相关依赖需要自己手动添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<dependencies>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
</dependencies>

工具实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140


import com.waysoft.modules.tools.FileUtil;
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;

import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

import java.io.File;

public class SlideToPdf implements Converter{

private static SlideToPdf slideToPdf = null;

private SlideToPdf(){}

public static SlideToPdf create(){
if(slideToPdf == null){
slideToPdf = new SlideToPdf();
}
return slideToPdf;
}

@Override
public byte[] convertToPdf(File file) throws Exception {
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream();
slideToPdf(new FileInputStream(file),outputStream,FileUtil.getFileExtension(file.getName()));
return outputStream.toByteArray();
}finally {
outputStream.close();
}
}



@Override
public byte[] convertToPdf(byte[] file) throws Exception {
return new byte[0];
}

private void slideToPdf(InputStream inStream, OutputStream outStream, String excelSuffix) throws Exception {
SlideShow slideShow = getPoiSlideByFileStream(inStream, excelSuffix);

Document pdfDocument = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, outStream);

AffineTransform affineTransform = new AffineTransform();
//设置基本字体
Font font = new Font(null,Font.PLAIN, 12);

Dimension pageSize = slideShow.getPageSize();
List<Slide> slideList = slideShow.getSlides();

pdfDocument.setPageSize(new Rectangle((float) pageSize.getWidth(), (float) pageSize.getHeight()));
pdfWriter.open();
pdfDocument.open();
PdfPTable table = new PdfPTable(1);
double zoom = 2;
affineTransform.setToScale(zoom, zoom);
for (Slide slide : slideList) {
List<Shape> shapesList = slide.getShapes();
for (Shape shape : shapesList) {

if ( shape instanceof XSLFTextShape) {
XSLFTextShape txtshape = (XSLFTextShape) shape;
for (XSLFTextParagraph textPara : txtshape.getTextParagraphs()) {
List<XSLFTextRun> textRunList = textPara.getTextRuns();
for (XSLFTextRun textRun : textRunList) {
textRun.setFontFamily("宋体");
}
}
}
if (shape instanceof HSLFTextShape){
HSLFTextShape txtshape = (HSLFTextShape) shape;
for (HSLFTextParagraph textPara : txtshape.getTextParagraphs()) {
List<HSLFTextRun> textRunList = textPara.getTextRuns();
for (HSLFTextRun textRun : textRunList) {
textRun.setFontFamily("宋体");
}
}
}
}

BufferedImage img = new BufferedImage((int) Math.ceil(pageSize.width * zoom), (int) Math.ceil(pageSize.height * zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setFont(font);
graphics.setTransform(affineTransform);

graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pageSize.width, pageSize.height));
slide.draw(graphics);
graphics.getPaint();
Image slideImage = Image.getInstance(img, null);
table.addCell(new PdfPCell(slideImage, true));
}
slideShow.close();
pdfDocument.add(table);
pdfDocument.close();
pdfWriter.close();

}

private SlideShow getPoiSlideByFileStream(InputStream inputStream, String excelSuffix) throws IOException {
SlideShow slideShow = null;
if (excelSuffix.equals("ppt")) {
slideShow = new HSLFSlideShow(inputStream);
}else {
slideShow = new XMLSlideShow(inputStream);
}
return slideShow;
}

}