1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java ppt转html_word ppt excel转pdf pdf转html工具类搭建

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

时间:2023-02-09 00:53:30

相关推荐

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

我看到很多需求要求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.mon.util;

import com.aspose.words.Document;

import com.aspose.words.License;

import com.aspose.words.SaveFormat;

import com.aspose.slides.*;

import mons.io.FileUtils;

import org.apache.log4j.Logger;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.text.DecimalFormat;

/**

* 说明:

*

* @version 创建时间: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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。