java ppt转html_word,ppt,excel转pdf,pdf转html工具类搭建

该博客介绍了如何使用Java实现PPT、Word、Excel到PDF的转换,以及PDF转HTML和图片的工具类。文章提供了具体的代码示例,依赖于Aspose库,并提到了需要的license文件。
摘要由CSDN通过智能技术生成

我看到很多需求要求word,excel,ppt,pptx转pdf等工具类。还有就是pdf转图片转html这里介绍一个这个工具类。

引入pom.xml

com.aspose

aspose-pdf

11.0.0

com.aspose

words

15.9.0

com.aspose

aspose-slides

15.9.0

工具类代码:

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这里就不公布了!

需要的通知请进群qq: 600922504

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值