java实现PPT转化为PDF

JACOB的方法,足可以解决这个问题,但是我既然以前曾经做过报表,就想尝试不同的方法。

JACOB是一座连接JAVA和微软的桥,所有的解析由微软解析。POI是没有微软解析的那么原汁原味的,所以如果要求高的话,还是使用JACOB。

大致思路很简单,将PPT先转化为图片,然后将图片写入PDF。转化图片是用POI,操作PDF使用ITEX。不过这个方法的BUG就是转化图片的POI效果不是很好。

导入的包分别是:itextpdf-5.1.3.jar,poi-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar。

然后贴代码了:

代码没有进行参数统一,写两个方法:

package com.zzk.cn;

import java.awt.Dimension;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import org.apache.poi.hslf.model.TextRun;

import org.apache.poi.hslf.record.Slide;

import org.apache.poi.hslf.usermodel.RichTextRun;

import org.apache.poi.hslf.usermodel.SlideShow;

public class PPTtoImage {

public static void main(String[] args) {

// 读入PPT文件

File file = new File("D:/书本JVM总结7-9.ppt");

doPPTtoImage(file);

}

public static boolean doPPTtoImage(File file) {

boolean isppt = checkFile(file);

if (!isppt) {

System.out.println("你指定的文件不是ppt文档!");

return false;

}

try {

FileInputStream is = new FileInputStream(file);

SlideShow ppt = new SlideShow(is);

is.close();

Dimension pgsize = ppt.getPageSize();

org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();

for (int i = 0; i < slide.length; i++) {

System.out.print("第" + i + "页。");

if (slide[i].getNotesSheet() != null

&& slide[i].getNotesSheet().getTextRuns() != null) {

// 获取第一个备注

System.out.println("备注:"

+ slide[i].getNotesSheet().getTextRuns()[0]

.getText());

}

TextRun[] truns = slide[i].getTextRuns();

for (int k = 0; k < truns.length; k++) {

RichTextRun[] rtruns = truns[k].getRichTextRuns();

for (int l = 0; l < rtruns.length; l++) {

rtruns[l].setFontIndex(1);

rtruns[l].setFontName("宋体");

// 获取文本列表

System.out.println(rtruns[l].getText());

}

}

BufferedImage img = new BufferedImage(pgsize.width,

pgsize.height, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics = img.createGraphics();

graphics.setPaint(Color.white);

graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,

pgsize.height));

slide[i].draw(graphics);

// 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径

FileOutputStream out = new FileOutputStream("D:/testImage/pict_"

+ (i + 1) + ".jpeg");

javax.imageio.ImageIO.write(img, "jpeg", out);

out.close();

}

System.out.println("ok");

return true;

} catch (FileNotFoundException e) {

System.out.println(e);

} catch (IOException e) {

e.printStackTrace();

}

return false;

}

// function 检查文件是否为PPT

public static boolean checkFile(File file) {

boolean isppt = false;

String filename = file.getName();

String suffixname = null;

if (filename != null && filename.indexOf(".") != -1) {

suffixname = filename.substring(filename.indexOf("."));

if (suffixname.equals(".ppt")) {

isppt = true;

}

return isppt;

} else {

return isppt;

}

}

}

第二段代码:

package com.zzk.cn;

import java.io.FileOutputStream;

import java.io.IOException;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Image;

import com.itextpdf.text.pdf.PdfWriter;

public class ImagetoPDF {

public static void main(String[] args) {

System.out.println("Chapter 6 example 3: using a relative path for HTML");

// step 1: creation of a document-object

Document document = new Document();

try {

// step 2:

// we create a writer that listens to the document

// and directs a PDF-stream to a file

PdfWriter.getInstance(document, new FileOutputStream("D:/测试图片.pdf"));

// HtmlWriter writer = HtmlWriter.getInstance(document, new FileOutputStream("Chap0603.html"));

// writer.setImagepath("../../images/kerstmis/");

// step 3: we open the document

document.open();

for(int i=1;i<=7;i++) {

// step 4: we add content

Image jpg = Image.getInstance("D:/testImage/pict_"+i+".jpeg");

jpg.scalePercent(50);

document.add(jpg);

}

}

catch(DocumentException de) {

System.err.println(de.getMessage());

}

catch(IOException ioe) {

System.err.println(ioe.getMessage());

}

// step 5: we close the document

document.close();

}

}

以上是 java实现PPT转化为PDF 的全部内容, 来源链接: utcz.com/z/344576.html

回到顶部