1.感謝
感謝小伙伴兒的分享:
● 不羈
● 郭中天
整合調(diào)整后的工具類(lèi)Gitee地址:https://gitee.com/yuanzhengme/java_application_aspose_demo
2.包含的工具類(lèi)
● WordToPdfUtil用于將word文檔轉(zhuǎn)換為pdf格式的工具類(lèi)
● ExcelToPdfUtil用于將excel文檔轉(zhuǎn)換為pdf格式的工具類(lèi)
● PdfToImageUtil用于將pdf文檔轉(zhuǎn)換為image格式的工具類(lèi)
3.lib文件說(shuō)明
3.1 使用的
● 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í)使用)
3.2 未使用的
● 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)換為其他格式【未破解效果依然不佳】
4.核心代碼
4.1 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();
}
}
}
4.2 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();
}
}
}
4.3 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();
}
}
6.問(wèn)題處理
- 都需要將字體文件
simsun.ttc
上傳到jarPath/font
目錄下。
6.1 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);
6.2 Excel中文無(wú)法轉(zhuǎn)換
使用aspose-cells-20.7.jar
:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-820858.html
<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)換方法里添加以下代碼:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-820858.html
// 設(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【代碼 16】將word、excel文件轉(zhuǎn)換為pdf格式和將pdf文檔轉(zhuǎn)換為image格式工具類(lèi)分享(Gitee源碼)aspose轉(zhuǎn)換中文亂碼問(wèn)題處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!