我看到很多需求要求word,excel,ppt,pptx转pdf等工具类。还有就是pdf转图片转html这里介绍一个这个工具类。
引入pom.xml
代码语言:javascript复制 <dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>11.0.0</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>words</artifactId>
<version>15.9.0</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>15.9.0</version>
</dependency>
工具类代码:
代码语言:javascript复制package com.lvic.prsp.common.util;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.aspose.slides.*;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
/**
* 说明:
*
* @version 创建时间:2016年9月16日 下午2:40:43
*/
public class FileConvertUtils {
private static Logger logger = Logger.getLogger(FileConvertUtils.class);
/**
* 获取word licence
*
* @return
*/
private static boolean getWordLicense() {
InputStream license = null;
try {
license = FileConvertUtils.class.getClassLoader().getResourceAsStream("license.xml");// license路径
License aposeLic = new License();
aposeLic.setLicense(license);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
return false;
} finally {
if (license != null) {
try {
license.close();
} catch (Exception ex) {
logger.info(ex);
}
}
}
return true;
}
/**
* 获取pdf licence
*
* @return
*/
private static boolean getPdfLicense() {
InputStream license = null;
try {
license = FileConvertUtils.class.getClassLoader().getResourceAsStream("license.xml");// license路径
com.aspose.pdf.License aposeLic = new com.aspose.pdf.License();
aposeLic.setLicense(license);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
return false;
} finally {
if (license != null) {
try {
license.close();
} catch (Exception ex) {
logger.info(ex);
}
}
}
return true;
}
/**
* 获取ppt licence
*
* @return
*/
private static boolean getPptLicense() {
InputStream license = null;
try {
license = FileConvertUtils.class.getClassLoader().getResourceAsStream("license.xml");// license路径
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(license);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
return false;
} finally {
if (license != null) {
try {
license.close();
} catch (Exception ex) {
logger.info(ex);
}
}
}
return true;
}
/**
* word转pdf
*
* @return
*/
public static boolean wordToPdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getWordLicense()) {
return false;
}
FileOutputStream os = null;
boolean res = false;
try {
os = new FileOutputStream(pdfPath);
// 转化的word文档
com.aspose.words.Document doc = new com.aspose.words.Document(wordPath);
doc.save(os, SaveFormat.PDF);
res = true;
} catch (Exception e) {
logger.error(e);
throw new RuntimeException(e);
} finally {
if (os != null) {
try {
os.close();
} catch (Exception ex) {
logger.info(ex);
}
}
}
return res;
}
/**
* ppt转pdf
*
* @return
*/
public static boolean pptToPdf(String pptPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getPptLicense()) {
return false;
}
FileOutputStream os = null;
boolean res = false;
try {
os = new FileOutputStream(pdfPath);
// 转化的ppt文档
com.aspose.slides.Presentation doc = new com.aspose.slides.Presentation(pptPath);
doc.save(os, com.aspose.slides.SaveFormat.Pdf);
res = true;
} catch (Exception e) {
logger.error(e);
throw new RuntimeException(e);
} finally {
if (os != null) {
try {
os.close();
} catch (Exception ex) {
logger.info(ex);
}
}
}
return res;
}
/**
* pdf转html
*
* @return
*/
public static boolean pdfToHtml(String pdfPath, String htmlPath) {
// 验证License
if (!getPdfLicense()) {
return false;
}
com.aspose.pdf.Document pdfDocument = null;
boolean res = false;
try {
//pdf文档
pdfDocument = new com.aspose.pdf.Document(pdfPath);
//html转换选项
com.aspose.pdf.HtmlSaveOptions options = new com.aspose.pdf.HtmlSaveOptions();
//光栅图像保存模式
options.RasterImagesSavingMode = com.aspose.pdf.HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
//保存文档
pdfDocument.save(htmlPath, options);
res = true;
} catch (Exception e) {
logger.info(pdfPath "转换pdf失败,目标路径:" htmlPath);
logger.info(e);
return false;
} finally {
if (pdfDocument != null) {
try {
pdfDocument.close();
} catch (Exception ex) {
logger.info(ex);
}
}
}
return res;
}
/**
* pdf转图片
*
* @param pdfPath
* @param imgPath
* @return
*/
public static String pdfToImages(String pdfPath, String imgPath, String imgPrefix) {
// 验证License
if (!getPdfLicense()) {
return null;
}
String imageList="";
com.aspose.pdf.Document doc = null;
try {
doc = new com.aspose.pdf.Document(pdfPath);
if (doc == null) {
throw new Exception("pdf文件无效或者pdf文件被加密!");
}
//从PDF文档的第几页开始转换
int startPageNum = 1;
//从PDF文档的第几页开始停止转换
int endPageNum = doc.getPages().size();
//设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024
com.aspose.pdf.devices.Resolution resolution = new com.aspose.pdf.devices.Resolution(128);
com.aspose.pdf.devices.JpegDevice jpegDevice = new com.aspose.pdf.devices.JpegDevice(resolution, 100);
for (int i = startPageNum; i <= endPageNum; i ) {
doc.getPages().get_Item(i).sendTo(jpegDevice, imgPath "/" imgPrefix "_" new DecimalFormat("000").format(i) ".jpg");
if (i == 0) {
imageList = imgPrefix "_" new DecimalFormat("000").format(i) ".jpg";
} else {
imageList = imageList "," imgPrefix "_" new DecimalFormat("000").format(i) ".jpg";
}
}
} catch (Exception ex) {
logger.info(ex);
} finally {
if (doc != null) {
try {
doc.close();
} catch (Exception ex) {
logger.info(ex);
}
}
}
return imageList;
}
public static void main(String[] args) {
FileConvertUtils utils = new FileConvertUtils();
utils.pptToPdf("D:\test.ppt","D:\ppttest1111111111.pdf");
}
}
这其中需要有license这里就不公布了!