使用 aspose 框架实现ex转pdf,图片。word转pdf,图片。pdf转图片
生活随笔
收集整理的這篇文章主要介紹了
使用 aspose 框架实现ex转pdf,图片。word转pdf,图片。pdf转图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用 aspose 框架實現(xiàn)ex轉(zhuǎn)pdf,圖片。word轉(zhuǎn)pdf,圖片。pdf轉(zhuǎn)圖片
下載好相應(yīng)的 aspose 的jar包加入maven依賴就可以直接使用,不過aspose項目是收費(fèi)項目在官網(wǎng)需要花錢購買。不想花錢的朋友也可以在網(wǎng)上找找破解版本
在使用中發(fā)現(xiàn)如果直接使用ex表格轉(zhuǎn)圖片或者直接使用word文件轉(zhuǎn)圖片。可能會存在格式上的差異。但是如果想將ex文件或者word文件轉(zhuǎn)為pdf文件在轉(zhuǎn)圖片的話,就不會存在這樣的差異。
經(jīng)測試該代碼和jar包都可以在linux系統(tǒng)上使用,但是要注意如果文件中存在中文,而linux系統(tǒng)中又不曾安裝中文字體,轉(zhuǎn)換出來的效果會不盡人意。下面附一個中文字體的包可以安裝到linux上,請自行下載
中文字體包
話不多說下面附上代碼
package com.utils;import com.aspose.cells.*; import com.aspose.words.*; import com.aspose.words.ImageSaveOptions; import com.aspose.words.License; import com.aspose.words.SaveFormat; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.poi.ss.formula.functions.T;import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List;public class WordtoPngUtils {static int flag = 1;//用來判斷文件是否刪除成功/*** licence 驗證* @return* @throws Exception*/public static boolean getLicense() throws Exception {boolean result = false;try {InputStream is = Document.class.getResourceAsStream("/com.aspose.words.lic_2999.xml");License aposeLic = new License();aposeLic.setLicense(is);result = true;is.close();} catch (Exception e) {// logger.error("License 獲取失敗");e.printStackTrace();throw e;}return result;}/**** @param path 文件地址* @param newFilePath 轉(zhuǎn)換以后的文件地址* @throws Exception*/public static void ExcelToPdf(String path, String newFilePath){String dir = newFilePath.substring(0, newFilePath.lastIndexOf("/") + 1);try {File file=new File(dir);if (!file.exists()) {file.mkdirs();}Workbook excel = null;excel = new Workbook(path);excel.save(newFilePath, com.aspose.cells.SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();}}/*** 轉(zhuǎn)換全部的pdf* @param filename PDF文件名*/public static List<String> pdf2png(String filename) {// 將pdf裝圖片 并且自定義圖片得格式大小List<String> list=new ArrayList<>();String type="jpg";File file = new File(filename);try {PDDocument doc = PDDocument.load(file);PDFRenderer renderer = new PDFRenderer(doc);int pageCount = doc.getNumberOfPages();for (int i = 0; i < pageCount; i++) {BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI// BufferedImage srcImage = resize(image, 240, 240);//產(chǎn)生縮略圖String img=filename.substring(0,filename.length()-5)+i+"."+type;ImageIO.write(image, type, new File(img));list.add(img);}doc.close();} catch (IOException e) {e.printStackTrace();}return list;}/*** @param filepath .xls或者.xlsx文件的路徑* @param picpath jpg或者png圖片的路徑*/public static void excelToImage (String filepath ,String picpath){Workbook book = null;try {book = new Workbook(filepath);Worksheet sheet = book.getWorksheets().get(0);ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();imgOptions.setImageFormat(ImageFormat.getJpeg());//imgOptions.setChartImageType(ImageFormat.getJpeg());imgOptions.setCellAutoFit(true);imgOptions.setOnePagePerSheet(true);imgOptions.setDefaultFont("200");SheetRender render = new SheetRender(sheet, imgOptions);render.toImage(0, picpath);} catch (Exception e) {e.printStackTrace();}}public static String getDataDir(Class c) {File dir = new File(System.getProperty("user.dir"));// System.out.println("shake" + dir.getAbsolutePath());dir = new File(dir, "src");dir = new File(dir, "main");dir = new File(dir, "resources");for (String s : c.getName().split("\\.")) {dir = new File(dir, s);}if (dir.exists()) {System.out.println("Using data directory: " + dir.toString());} else {dir.mkdirs();System.out.println("Creating data directory: " + dir.toString());}return dir.toString() + File.separator;}/*** 文檔轉(zhuǎn)圖片* @param inPath 傳入文檔地址* @param outDir 輸出的圖片文件夾地址*/public static List<String> doc2Img(String inPath, String outDir) {List<String> list=new ArrayList<>();try {if (!getLicense()) {throw new Exception("com.aspose.words lic ERROR!");}//logger.info(inPath + " -> " + outDir);long old = System.currentTimeMillis();// word文檔Document doc = new Document(inPath);// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS轉(zhuǎn)換ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);int pageCount = doc.getPageCount();for (int i = 0; i < pageCount; i++) {String s=outDir.substring(0,outDir.length()-5) + i + ".jpg";File file = new File(s);list.add(s);//logger.info(outDir + "/" + i + ".png");FileOutputStream os = new FileOutputStream(file);options.setPageIndex(i);doc.save(os, options);os.close();}long now = System.currentTimeMillis();//logger.info("convert OK! " + ((now - old) / 1000.0) + "秒");} catch (Exception e) {e.printStackTrace();}return list;}/*** word轉(zhuǎn)pdf的方法* @param inPath* @param outPath*/public static void doc2pdf(String inPath, String outPath) {System.out.println(inPath + " -> " + outPath);File files=new File(outPath);//如果這個pdf存在就不轉(zhuǎn)換他if (!files.exists()){try {if (!getLicense()) { // 驗證License 若不驗證則轉(zhuǎn)化出的pdf文檔有水印throw new Exception("com.aspose.words lic ERROR!");}long old = System.currentTimeMillis();Document doc = new Document(inPath);File file = new File(outPath);FileOutputStream os = new FileOutputStream(file);// word文檔// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS轉(zhuǎn)換doc.save(os, SaveFormat.PDF);os.close(); // long now = System.currentTimeMillis(); // System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");} catch (Exception e) {e.printStackTrace();}}}/*** 下載網(wǎng)文件* @param path 資源的路徑* @param filepath 文件存放的目錄* @param fileip 生成文件最終的地址* @throws MalformedURLException*/public static void downloadNet(String path, String filepath,String fileip) throws MalformedURLException {// 下載網(wǎng)絡(luò)文件int bytesum = 0;int byteread = 0;URL url = new URL(path);File file=new File(filepath);if (!file.exists()) {file.mkdirs();}File files=new File(fileip);if (!files.exists()){try {URLConnection conn = url.openConnection();InputStream inStream = conn.getInputStream();FileOutputStream fs = new FileOutputStream(fileip);byte[] buffer = new byte[1204];int length;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread;// System.out.println(bytesum);fs.write(buffer, 0, byteread);}fs.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}/*** 刪除指定文件* @param file* @return*/public static Boolean deleteFile(File file){//判斷文件不為null或文件目錄存在if (file == null || !file.exists()){flag = 0;System.out.println("文件刪除失敗,請檢查文件路徑是否正確");return true;}//取得這個目錄下的所有子文件對象File[] files = file.listFiles();//遍歷該目錄下的文件對象for (File f: files){//打印文件名String name = file.getName();System.out.println(name);//判斷子目錄是否存在子目錄,如果是文件則刪除if (f.isDirectory()){deleteFile(f);}else {f.delete();}}//刪除空文件夾 for循環(huán)已經(jīng)把上一層節(jié)點的目錄清空。file.delete();return true;}/*** 分割文件路徑* @param path* @return*/public static String tupath(String path){String[] split = path.split("/");String s="/";for (int i = 0; i < split.length-1; i++) {if (i==split.length-2){s=s+split[i];}else {s=s+split[i]+"/";}}return s;} }最后加上依賴的?aspose 包版本
aspose-words-18.6-jdk16 Aspose-cells-20.4-crack.jar總結(jié)
以上是生活随笔為你收集整理的使用 aspose 框架实现ex转pdf,图片。word转pdf,图片。pdf转图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10多合一原版系统_win10多合
- 下一篇: Kettle使用教程之Job使用