aspose实现word转pdf
1.引入jar包
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>19.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose.slides-19.3.jar</systemPath>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose.pdf</artifactId>
<version>19.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose-pdf-19.3.jar</systemPath>
</dependency>
2.转换工具类
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class AsposeToPdfUtil {
/**
* 获取license
*
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
// license.xml路径
ClassPathResource cp = new ClassPathResource("license.xml");
InputStream is = cp.getInputStream();
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 文件转PDF
*
* @return
*/
public static void file2pdf(String inPath, String outPath) {
String suffix = getFileSufix(inPath);
if (suffix.equals("pdf")) {
return ;
}
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return ;
}
InputStream is =null;
FileOutputStream os =null;
try {
ClassPathResource cp = new ClassPathResource("license.xml");
is=cp.getInputStream();
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
os=new FileOutputStream(file);
if (suffix.equals("doc") || suffix.equals("docx")) {
com.aspose.words.License wordLicense = new com.aspose.words.License();
wordLicense.setLicense(is);
Document doc = null;
//解决linux下word转pdf中文乱码问题---1.给linux系统安装windows字体库;2.设置引用该字体库
String osName = System.getProperty("os.name");
if(!osName.toLowerCase().startsWith("win")){
FontSettings.setFontsFolder(File.separator + "usr" + File.separator + "share" + File.separator + "fonts" +File.separator + "winfonts"+File.separator + "winfonts", true);
}
doc = new Document(inPath); // inPath是将要被转化的word文档
doc.save(os, com.aspose.words.SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
} else if (suffix.equals("ppt") || suffix.equals("pptx")) {
com.aspose.slides.License pptLicense = new com.aspose.slides.License();
pptLicense.setLicense(is);
Presentation doc = new Presentation(inPath);
doc.save(os, com.aspose.slides.SaveFormat.Pdf);
} else if (suffix.equals("xls") || suffix.equals("xlsx")|| suffix.equals("xlsm")) {
com.aspose.cells.License excelLicense = new com.aspose.cells.License();
excelLicense.setLicense(is);
Workbook book = new Workbook(inPath);
book.save(os, com.aspose.cells.SaveFormat.PDF);
} else {
System.out.println("请上传WORLD, EXCEL, PPT,PDF格式文件");
return ;
}
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
is.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
}
}
}
/**
* word转PDF
*
* @return
*/
public static void doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return ;
}
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(inPath); // inPath是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
public static void savedocx(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, SaveFormat.DOCX);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1).toLowerCase(); // 获取文件后缀;
}
}
3.使用工具,word转pdf
//office转pdf--aspose版
String docPath=absolutePath + fileName;
String pdfPath= absolutePath+fileName.substring(0,fileName.lastIndexOf("."))+".pdf" ;
AsposeToPdfUtil.file2pdf(docPath,pdfPath);