基于Java實(shí)現(xiàn)將word,excel文件轉(zhuǎn)換為pdf的工具類(lèi)
包含的工具類(lèi)
WordToPdfUtil用于將word文檔轉(zhuǎn)換為pdf格式的工具類(lèi)
ExcelToPdfUtil用于將excel文檔轉(zhuǎn)換為pdf格式的工具類(lèi)
PdfToImageUtil用于將pdf文檔轉(zhuǎn)換為image格式的工具類(lèi)
lib文件說(shuō)明
使用的
aspose-words-15.8.0-jdk16.jar 將word文檔轉(zhuǎn)換為pdf需要引入
aspose-cells-8.5.2.jar 將excel文檔轉(zhuǎn)換為pdf需要引入
aspose-cells-20.7.jar 將excel文檔轉(zhuǎn)換為pdf需要引入(Linux端中文出現(xiàn)亂碼時(shí)使用)
未使用的
aspose-words-15.12.0-jdk16.jar 未測(cè)試
aspose-pdf-22.4.cracked.jar 將pdf轉(zhuǎn)換為其他格式【破解版效果不佳】
aspose-pdf-22.4.jar 將pdf轉(zhuǎn)換為其他格式【未破解效果依然不佳】
核心代碼
WordToPdfUtil
/**
* word 轉(zhuǎn) pdf
*
* @param wordFilePath word文件路徑
* @param pdfFilePath pdf文件路徑
*/
public static void convert(String wordFilePath, String pdfFilePath) {
FileOutputStream fileOutputStream = null;
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(wordFilePath) : pdfFilePath;
setLicense();
File file = new File(pdfFilePath);
fileOutputStream = new FileOutputStream(file);
Document doc = new Document(wordFilePath);
doc.save(fileOutputStream, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fileOutputStream != null;
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ExcelToPdfUtil
/**
* excel 轉(zhuǎn) pdf
*
* @param excelFilePath excel文件路徑
* @param pdfFilePath pdf文件路徑
* @param convertSheets 需要轉(zhuǎn)換的sheet
*/
public static void convert(String excelFilePath, String pdfFilePath, int[] convertSheets) {
FileOutputStream fileOutputStream = null;
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 設(shè)置License
setLicense();
// 讀取excel文件
Workbook wb = new Workbook(excelFilePath);
fileOutputStream = new FileOutputStream(pdfFilePath);
// 設(shè)置pdf格式
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOutputStream, pdfSaveOptions);
fileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fileOutputStream != null;
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
PdfToImageUtil
/**
* 根據(jù)參數(shù)將全部的PDF轉(zhuǎn)換為Image
*
* @param pdfFilePath PDF文件路徑
* @param imageFileDir 圖片存儲(chǔ)目錄
* @param imageFileName 圖片存儲(chǔ)文件沒(méi)
* @param type 圖片類(lèi)型
*/
public static void convertAllPage(String pdfFilePath, String imageFileDir, String imageFileName, String type) {
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
// 圖片類(lèi)型
if (type == null || "".equals(type)) {
type = IMAGE_TYPE_JPG;
}
// 1.加載PDF文件
File file = new File(pdfFilePath);
// 2.生成JPG圖片的文件夾
imageFileDir = imageFileDir == null ? getImageFileDir(pdfFilePath) : imageFileDir;
imageFileName = imageFileName == null ? getImageFileName(pdfFilePath) : imageFileName;
try {
PDDocument pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pageCount = pdDocument.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144);
ImageIO.write(image, type,
new File(imageFileDir.concat(File.separator).concat(imageFileName).concat("_")
.concat(String.valueOf(i + 1)).concat(".").concat(type)));
}
} catch (IOException e) {
e.printStackTrace();
}
}
問(wèn)題處理
都需要將字體文件simsun.ttc上傳到jarPath/font目錄下。
Word中文無(wú)法轉(zhuǎn)換
在Linux環(huán)境下,如果轉(zhuǎn)換后的pdf文件無(wú)中文,在WordToPdfUtil轉(zhuǎn)換方法里添加以下代碼:
// 設(shè)置字體 String realPath = new ApplicationHome(WordToPdfUtil.class).getSource().getParentFile().toString(); FontSettings.setFontsFolder(realPath + File.separatorChar + "font", false);
Excel中文無(wú)法轉(zhuǎn)換
使用aspose-cells-20.7.jar:
<dependency>
<groupId>com.aspose.cells</groupId>
<artifactId>aspose-cells</artifactId>
<version>20.7</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-cells-20.7.jar</systemPath>
</dependency>
并在ExcelToPdfUtil轉(zhuǎn)換方法里添加以下代碼:
// 設(shè)置字體 String realPath = new ApplicationHome(WordToPdfUtil.class).getSource().getParentFile().toString(); String fontDir = realPath + File.separatorChar + "font"; IndividualFontConfigs individualFontConfigs = new IndividualFontConfigs(); individualFontConfigs.setFontFolder(fontDir, false); LoadOptions loadOptions = new LoadOptions(); loadOptions.setFontConfigs(individualFontConfigs); // 讀取excel文件 Workbook wb = new Workbook(excelFilePath, loadOptions);
7.總結(jié)
PDF轉(zhuǎn)換為其他格式的方法效果不佳,遇到好的方案會(huì)進(jìn)行補(bǔ)充。
主要用到aspose的jar包,實(shí)際上是需要授權(quán)的,否則會(huì)有水印,是個(gè)隱患。
到此這篇關(guān)于基于Java實(shí)現(xiàn)將word,excel文件轉(zhuǎn)換為pdf的工具類(lèi)的文章就介紹到這了,更多相關(guān)Java word與excel轉(zhuǎn)pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Lambda表達(dá)式與匿名內(nèi)部類(lèi)的聯(lián)系和區(qū)別實(shí)例分析
這篇文章主要介紹了Java Lambda表達(dá)式與匿名內(nèi)部類(lèi)的聯(lián)系和區(qū)別,結(jié)合實(shí)例形式分析了Java Lambda表達(dá)式與匿名內(nèi)部類(lèi)功能、用法、區(qū)別及操作注意事項(xiàng),需要的朋友可以參考下2019-10-10
解決JMap抓取heap使用統(tǒng)計(jì)信息報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決JMap抓取heap使用統(tǒng)計(jì)信息報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Java利用Geotools實(shí)現(xiàn)不同坐標(biāo)系之間坐標(biāo)轉(zhuǎn)換
GeoTools 是一個(gè)開(kāi)源的 Java GIS 工具包,可利用它來(lái)開(kāi)發(fā)符合標(biāo)準(zhǔn)的地理信息系統(tǒng)。本文將利用工具包Geotools實(shí)現(xiàn)不同坐標(biāo)系之間坐標(biāo)轉(zhuǎn)換,感興趣的可以了解一下2022-08-08
Java Fluent Mybatis 聚合查詢(xún)與apply方法詳解流程篇
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國(guó)內(nèi)又以Mybatis用的多,基于mybatis上的增強(qiáng)框架,又有mybatis plus和TK mybatis等。今天我們介紹一個(gè)新的mybatis增強(qiáng)框架 fluent mybatis關(guān)于聚合查詢(xún)、apply方法詳解2021-10-10
SpringBoot定義Bean的幾種實(shí)現(xiàn)方式
本文主要介紹了SpringBoot定義Bean的幾種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Springboot @WebFilter無(wú)法注入其他Bean的示例問(wèn)題
這篇文章主要介紹了Springboot @WebFilter無(wú)法注入其他Bean的示例問(wèn)題,本文通過(guò)示例代碼給大家分享解決方法,需要的朋友可以參考下2021-09-09
java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同
這篇文章主要介紹了java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同的相關(guān)資料,需要的朋友可以參考下2017-01-01

