包含的工具类
wordtopdfutil用于将word文档转换为pdf格式的工具类
exceltopdfutil用于将excel文档转换为pdf格式的工具类
pdftoimageutil用于将pdf文档转换为image格式的工具类
lib文件说明
使用的
aspose-words-15.8.0-jdk16.jar 将word文档转换为pdf需要引入
aspose-cells-8.5.2.jar 将excel文档转换为pdf需要引入
aspose-cells-20.7.jar 将excel文档转换为pdf需要引入(linux端中文出现乱码时使用)
未使用的
aspose-words-15.12.0-jdk16.jar 未测试
aspose-pdf-22.4.cracked.jar 将pdf转换为其他格式【破解版效果不佳】
aspose-pdf-22.4.jar 将pdf转换为其他格式【未破解效果依然不佳】
核心代码
wordtopdfutil
/**
* word 转 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 转 pdf
*
* @param excelfilepath excel文件路径
* @param pdffilepath pdf文件路径
* @param convertsheets 需要转换的sheet
*/
public static void convert(string excelfilepath, string pdffilepath, int[] convertsheets) {
fileoutputstream fileoutputstream = null;
try {
pdffilepath = pdffilepath == null ? getpdffilepath(excelfilepath) : pdffilepath;
// 设置license
setlicense();
// 读取excel文件
workbook wb = new workbook(excelfilepath);
fileoutputstream = new fileoutputstream(pdffilepath);
// 设置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
/**
* 根据参数将全部的pdf转换为image
*
* @param pdffilepath pdf文件路径
* @param imagefiledir 图片存储目录
* @param imagefilename 图片存储文件没
* @param type 图片类型
*/
public static void convertallpage(string pdffilepath, string imagefiledir, string imagefilename, string type) {
system.setproperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.kcmsserviceprovider");
// 图片类型
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();
}
}
问题处理
都需要将字体文件simsun.ttc上传到jarpath/font目录下。
word中文无法转换
在linux环境下,如果转换后的pdf文件无中文,在wordtopdfutil转换方法里添加以下代码:
// 设置字体 string realpath = new applicationhome(wordtopdfutil.class).getsource().getparentfile().tostring(); fontsettings.setfontsfolder(realpath + file.separatorchar + "font", false);
excel中文无法转换
使用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转换方法里添加以下代码:
// 设置字体 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.总结
pdf转换为其他格式的方法效果不佳,遇到好的方案会进行补充。
主要用到aspose的jar包,实际上是需要授权的,否则会有水印,是个隐患。
到此这篇关于基于java实现将word,excel文件转换为pdf的工具类的文章就介绍到这了,更多相关java word与excel转pdf内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论