word转pdf工具类
关键代码片段
public static void main(String[] args) {
String docPath = "D:\\测试文件.docx";
String pdfPath = "D:\\测试文件.pdf";
docToPdf(docPath, pdfPath);
// String excelPath = "D:\\测试文件.xls";
// String pdfPath = "D:\\测试文件.pdf";
// excelToPdf(excelPath, pdfPath);
}
/**
* word转pdf
* @param wordPath word文件路径
* @param pdfPath pdf文件路径
*/
public synchronized static void docToPdf(String wordPath, String pdfPath) {
if(!getLicense()) {
return;
}
try {
long old = System.currentTimeMillis();
File file = new File(pdfPath);
String os = System.getProperty("os.name");
System.out.println(os);
if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
FontSettings.setFontsFolder(path, true);
}
FileOutputStream stream = new FileOutputStream(file);
Document document = new Document(wordPath);
// 全面支持DOC、DOCX、
document.save(stream, SaveFormat.PDF);
stream.close();
long now = System.currentTimeMillis();
System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
}
catch (Exception e) {
e.printStackTrace();
}
}
完整代码如下:
- package com.xxx.common.utils;
-
- import com.aspose.cells.PdfSaveOptions;
- import com.aspose.cells.Workbook;
- import com.aspose.words.Document;
- import com.aspose.words.FontSettings;
- import com.aspose.words.License;
- import com.aspose.words.SaveFormat;
-
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
-
- /**
- * Pdf工具类
- */
- public class PDFUtil {
-
- /**
- * 获取验证授权
- */
- private static boolean getLicense() {
- boolean result = false;
- try {
- InputStream stream = PDFUtil.class.getClassLoader().getResourceAsStream("license.xml");
- License license = new License();
- license.setLicense(stream);
- result = true;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
-
- /**
- * word转pdf
- * @param wordPath word文件路径
- * @param pdfPath pdf文件路径
- */
- public synchronized static void docToPdf(String wordPath, String pdfPath) {
- if(!getLicense()) {
- return;
- }
- try {
- long old = System.currentTimeMillis();
- File file = new File(pdfPath);
- String os = System.getProperty("os.name");
- System.out.println(os);
- if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
- String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
- FontSettings.setFontsFolder(path, true);
- }
- FileOutputStream stream = new FileOutputStream(file);
- Document document = new Document(wordPath);
- // 全面支持DOC、DOCX、
- document.save(stream, SaveFormat.PDF);
- stream.close();
- long now = System.currentTimeMillis();
- System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * xceld转pdf
- * @param excelPath excel文件路径
- * @param pdfPath pdf文件路径
- */
- public synchronized static void excelToPdf(String excelPath, String pdfPath) {
- if(!getLicense()) {
- return;
- }
- try {
- long old = System.currentTimeMillis();
- Workbook workbook = new Workbook(excelPath);
-
- String os = System.getProperty("os.name");
- System.out.println(os);
- if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
- String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
- FontSettings.setFontsFolder(path, true);
- }
- FileOutputStream stream = new FileOutputStream(pdfPath);
- workbook.save(stream, com.aspose.cells.SaveFormat.PDF);
- stream.close();
- long now = System.currentTimeMillis();
- System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * xceld转pdf
- * @param excelPath excel文件路径
- * @param pdfPath pdf文件路径
- */
- public synchronized static void excelToPdfTwo(String excelPath, String pdfPath) {
- if(!getLicense()) {
- return;
- }
- try {
- long old = System.currentTimeMillis();
- Workbook workbook = new Workbook(excelPath);
- String os = System.getProperty("os.name");
- System.out.println(os);
- if(!ToolsUtil.isEmpty(os) && !os.toLowerCase().contains("win")) {
- String path = File.separator + "usr" + File.separator + "share" + File.separator + "fonts" + File.separator + "win";
- FontSettings.setFontsFolder(path, true);
- }
- FileOutputStream stream = new FileOutputStream(pdfPath);
- PdfSaveOptions pdfSaveOptions=new PdfSaveOptions();
- pdfSaveOptions.setOnePagePerSheet(true);
- //当excel对应的页宽度太大时,在pdf中会拆断并分页,此处等比缩放
- int[] autoDrawSheets={3};
- autoDraw(workbook,autoDrawSheets);
-
- workbook.save(stream, pdfSaveOptions);
- stream.close();
- long now = System.currentTimeMillis();
- System.out.println("共计耗时:" + ((now - old) / 1000.0) + "秒");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- public static void autoDraw(Workbook wb,int[] page){
- if(null!=page && page.length>0){
- for (int i=0;i
- wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
- wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
- }
- }
- }
-
- public static void main(String[] args) {
- String docPath = "D:\\测试文件.docx";
- String pdfPath = "D:\\测试文件.pdf";
- docToPdf(docPath, pdfPath);
-
- // String excelPath = "D:\\测试文件.xls";
- // String pdfPath = "D:\\测试文件.pdf";
- // excelToPdf(excelPath, pdfPath);
- }
-
- }