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.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
總結
以上是生活随笔為你收集整理的java ppt转html_word,ppt,excel转pdf,pdf转html工具类搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux将汇编转为机器码,汇编语言 高
- 下一篇: 中国区独占!Intel锐炫A380显卡偷