1. 程式人生 > 其它 >word和excel轉pdf

word和excel轉pdf

技術標籤:成長筆記

excel有水印,歡迎大佬解決@!!!!
支援word和excel新版老闆格式!!!1

需要的jar包:百度能找到,免費上傳資源已存在傳不了
在這裡插入圖片描述


import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.License;

import java.io.*;

/**
 * @description:
 * @author: Jhang
 * @time: 2020/12/7 16:32
 */
public class ExcelAndWorldToPdf { /** * 獲取license 主要除去水印 * @return */ private static boolean getLicense() { boolean result = false; try { // 憑證 String licenseStr = "<License>\n" + " <Data>\n"
+ " <Products>\n" + " <Product>Aspose.Total for Java</Product>\n" + " <Product>Aspose.Words for Java</Product>\n" + " </Products>\n"
+ " <EditionType>Enterprise</EditionType>\n" + " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" + " <LicenseExpiry>20991231</LicenseExpiry>\n" + " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" + " </Data>\n" + " <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" + "</License>"; InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8")); License asposeLic = new License(); asposeLic.setLicense(license); result = true; } catch (Exception e) { } return result; } /** * excel轉pdf 支援xls或者xlsx * * @param sourceExcel excel檔案具體地址,N:/sourceExcel * @param targetPdf 轉換後pdf具體地址,N:/target.pdf * @return * @auth xll */ public static String excel2pdf(String sourceExcel, String targetPdf) throws IOException { if (!getLicense()) { //水印沒有消除 return "failed:can't find aspose license"; } FileOutputStream os = null; try { long start = System.currentTimeMillis(); //新建一個空白pdf文件 File file = new File(targetPdf); os = new FileOutputStream(file); Workbook wb = new Workbook(sourceExcel); wb.save(os, SaveFormat.PDF); long end = System.currentTimeMillis(); System.out.println("excel轉pdf共耗時:" + ((end - start) / 1000.0) + "秒"); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { os.close(); } } return "successed"; } /** * word轉pdf 支援doc或者docx * * @param sourceDoc word檔案具體地址,N:/source.dox * @param targetPdf 轉換後pdf具體地址,N:/target.pdf * @return * @auth xll */ public static String doc2pdf(String sourceDoc, String targetPdf) throws IOException { if (!getLicense()) { // 驗證License 若不驗證則轉化出的pdf文件會有水印產生 return "failed:can't find aspose license"; } FileOutputStream os = null; try { long start = System.currentTimeMillis(); //新建一個空白pdf文件 File file = new File(targetPdf); os = new FileOutputStream(file); Document doc = new Document(sourceDoc); doc.save(os, com.aspose.words.SaveFormat.PDF); doc.protect(1,"1111"); long end = System.currentTimeMillis(); System.out.println("word轉pdf共耗時:" + ((end - start) / 1000.0) + "秒"); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { os.close(); } } return "successed"; } public static void main(String[] args) throws IOException { //excel2pdf("F:\\exce\\test\\pdf\\FM.xlsx", "F:\\exce\\test\\pdf\\ToPDF.pdf"); // excel2pdf("F:\\exce\\test\\pdf\\測試.xls", "F:\\exce\\test\\pdf\\NOE1.pdf"); // doc2pdf("F:\\exce\\test\\pdf\\test01.docx", "F:\\exce\\test\\pdf\\wold01ToPDF.pdf"); // doc2pdf("F:\\exce\\test\\pdf\\test.doc", "F:\\exce\\test\\pdf\\wold02ToPDF.pdf"); } }