import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.aspose.words.Document; import com.aspose.words.SaveFormat; public class PdfUtil { //判斷本地文件是否存在 @RequestMapping(params = "fileexists") @ResponseBody public boolean Fileexists(String filename) { System.out.println("filename------->"+filename); File file = new File(filename); if (file.exists()) { System.out.println("存在"); return true; } else { System.out.println("不存在"); return false; } } /** * word to pdf * @param inPath word 全路徑 * @param outPath 生成的pdf 全路徑 * @author an * @throws Exception */ public String doc2pdf(String inPath, String outPath) throws Exception { String path = outPath.substring(0, outPath.lastIndexOf("/")); File file = null; file = new File(path); if (!file.exists()) {//創建文件夾 file.mkdirs(); } file = new File(outPath); //新建一個空白pdf文檔 FileOutputStream os = new FileOutputStream(file); Document doc = new Document(inPath); // Address是將要被轉化的word文檔 doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, // EPUB, XPS, SWF 相互轉換 os.close(); return null; } /** * pdf轉圖片,返回文件保存的路徑 * @param inputFile 輸入需要轉的pdf全路徑 * @param outputUrl 轉換完成后保存的鏈接 * @throws Exception */ public int pdfToImage(String inputFile, String outputUrl) throws Exception { int i; File file = new File(inputFile); File output=new File(outputUrl); //如果保存路徑不存在,創建路徑 if(!output.exists()){ output.mkdirs(); } PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); String[] urls=new String[pageCount]; for ( i = 0; i < pageCount; i++) { //設置圖片轉換的分辨率 BufferedImage image = renderer.renderImage(i, 1.5f); //文件保存的路徑 urls[i]= (i + 1) + ".png"; ImageIO.write(image, "PNG", new File(outputUrl+urls[i])); } System.out.println("i===========>"+i); System.out.println("轉化成功"); return i; } /** * pdf 轉換成 一張圖片 * @param pdfFile * @param outpath */ public void pdf2multiImage(String pdfFile, String outpath) { try { InputStream is = new FileInputStream(pdfFile); PDDocument pdf = PDDocument.load(is); int actSize = pdf.getNumberOfPages(); List<BufferedImage> piclist = new ArrayList<BufferedImage>(); for (int i = 0; i < actSize; i++) { BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i,130, ImageType.RGB); piclist.add(image); } File output=new File(outpath); //如果保存路徑不存在,創建路徑 if(!output.exists()){ output.mkdirs(); } yPic(piclist, outpath); is.close(); System.out.println("success"); } catch (IOException e) { e.printStackTrace(); } } public static void pdf2multiImage(InputStream is, String outpath) { try { PDDocument pdf = PDDocument.load(is); int actSize = pdf.getNumberOfPages(); List<BufferedImage> piclist = new ArrayList<BufferedImage>(); for (int i = 0; i < actSize; i++) { BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i,130, ImageType.RGB); piclist.add(image); } yPic(piclist, outpath); is.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 將寬度相同的圖片,豎向追加在一起 ##注意:寬度必須相同 * * @param piclist * 文件流數組 * @param outPath * 輸出路徑 */ public static void yPic(List<BufferedImage> piclist, String outPath) {// 縱向處理圖片 if (piclist == null || piclist.size() <= 0) { System.out.println("圖片數組為空!"); return; } try { int height = 0, // 總高度 width = 0, // 總寬度 _height = 0, // 臨時的高度 , 或保存偏移高度 __height = 0, // 臨時的高度,主要保存每個高度 picNum = piclist.size();// 圖片的數量 int[] heightArray = new int[picNum]; // 保存每個文件的高度 BufferedImage buffer = null; // 保存圖片流 List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的圖片的RGB int[] _imgRGB; // 保存一張圖片中的RGB數據 for (int i = 0; i < picNum; i++) { buffer = piclist.get(i); heightArray[i] = _height = buffer.getHeight();// 圖片高度 if (i == 0) { width = buffer.getWidth();// 圖片寬度 } height += _height; // 獲取總高度 _imgRGB = new int[width * _height];// 從圖片中讀取RGB _imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width); imgRGB.add(_imgRGB); } _height = 0; // 設置偏移高度為0 // 生成新圖片 BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < picNum; i++) { __height = heightArray[i]; if (i != 0) _height += __height; // 計算偏移高度 imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 寫入流中 } File outFile = new File(outPath); ImageIO.write(imageResult, "png", outFile);// 寫圖片 } catch (Exception e) { e.printStackTrace(); } } }
Aspose.Words jar包:
鏈接:https://pan.baidu.com/s/1xIq7G_sqKPNULtIt8VZtPQ
提取碼:ykf6