word,ppt等office软件转化为pdf进行展示(POI +iText)(亲测有效)
废话不多说,上代码
1. ppt转化为pdf,利用java的POI和itext进行转化
/**
* 返回pdf文件
*/
public File convertPPTToPDF(File file, File toFile) {
try {
Document pdfDocument = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(toFile));
FileInputStream is = new FileInputStream(file);
double zoom = 2;
XMLSlideShow ppt = convertPPTToPDFByPPTX(is);
if (ppt == null) {
throw new NullPointerException("This PPTX get data is error....");
}
Dimension pgsize = ppt.getPageSize();
XSLFSlide slide[] = ppt.getSlides();
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
pdfDocument.setPageSize(new com.itextpdf.text.Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
pdfWriter.open();
pdfDocument.open();
PdfPTable table = new PdfPTable(1);
for (int i = 0; i < slide.length; i++) {
for (XSLFShape shape : slide[i].getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape txtshape = (XSLFTextShape) shape;
for (XSLFTextParagraph textPara : txtshape.getTextParagraphs()) {
List<XSLFTextRun> textRunList = textPara.getTextRuns();
for (XSLFTextRun textRun : textRunList) {
textRun.setFontFamily("宋体");
}
}
}
}
BufferedImage img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
graphics.getPaint();
com.itextpdf.text.Image slideImage = Image.getInstance(img, null);
table.addCell(new PdfPCell(slideImage, true));
}
pdfDocument.add(table);
pdfDocument.close();
pdfWriter.close();
log.info(file.getAbsolutePath() + "Powerpoint file converted to PDF successfully");
return toFile;
} catch (Exception e) {
log.info(file.getAbsolutePath() + "--->" + e.getMessage());
return null;
}
}
private XMLSlideShow convertPPTToPDFByPPTX(FileInputStream is) {
try {
return new XMLSlideShow(is);
} catch (IOException e) {
return null;
}
}
2.wordtopdf
/**
* word to pdf
*/
public File convertWordToPDF(File file, File toFile) {
try {
// size of pdfDocument
Document pdfDocument = new Document(PageSize.A3, 72, 72, 72, 72);
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(toFile));
FileInputStream input_document = new FileInputStream(file);
XWPFDocument doc = new XWPFDocument(input_document);
//72 units=1 inch
pdfWriter.setInitialLeading(20);
//get all paragraphs from word docx
List<XWPFParagraph> plist = doc.getParagraphs();
//open pdf document for writing
pdfWriter.open();
pdfDocument.open();
for (int i = 0; i < plist.size(); i++) {
//read through the list of paragraphs
XWPFParagraph pa = plist.get(i);
//get all run objects from each paragraph
List<XWPFRun> runs = pa.getRuns();
//read through the run objects
for (int j = 0; j < runs.size(); j++) {
XWPFRun run = runs.get(j);
//get pictures from the run and add them to the pdf document
List<XWPFPicture> piclist = run.getEmbeddedPictures();
//traverse through the list and write each image to a file
Iterator<XWPFPicture> iterator = piclist.iterator();
while (iterator.hasNext()) {
XWPFPicture pic = iterator.next();
XWPFPictureData picdata = pic.getPictureData();
byte[] bytepic = picdata.getData();
Image imag = Image.getInstance(bytepic);
pdfDocument.add(imag);
}
// 中文字体的解决 Font_Songti为字体的配置文件
BaseFont bf = BaseFont.createFont(fileProperties.getPropertiesByKey("Font_Songti"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 15.0f, Font.NORMAL, BaseColor.BLACK);
//construct unicode string
String text = run.getText(-1);
byte[] bs;
if (text != null) {
bs = text.getBytes();
String str = new String(bs);
//add string to the pdf document
Chunk chObj1 = new Chunk(str, font);
pdfDocument.add(chObj1);
}
}
//output new line
pdfDocument.add(new Chunk(Chunk.NEWLINE));
}
//close pdf document
pdfDocument.close();
pdfWriter.close();
log.info(file.getAbsolutePath() + "Powerpoint file converted to PDF successfully");
return toFile;
} catch (Exception e) {
log.info(file.getAbsolutePath() + "--->" + e.getMessage());
return null;
}
}
3.处理pptx的中文乱码的问题
for (int i = 0; i < slide.length; i++) {
for (XSLFShape shape : slide[i].getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape txtshape = (XSLFTextShape) shape;
for (XSLFTextParagraph textPara : txtshape.getTextParagraphs()) {
List<XSLFTextRun> textRunList = textPara.getTextRuns();
for (XSLFTextRun textRun : textRunList) {
textRun.setFontFamily("宋体");
}
}
}
}