????????圖片和PDF是我們日常生活和工作中經常接觸到的文檔格式。PDF是人們日常使用最多的跨平臺文檔,是一種用獨立于應用程序、硬件、操作系統的方式呈現文檔的文件格式。每個PDF文件包含固定布局的平面文檔的完整描述,包括文本、字形、圖形及其他需要顯示的信息。然而,從圖片或PDF中提取出關鍵內容并不是一件簡單的事情。
????????最近開發項目需要抽取PDF中的圖片,做了一些研究,記錄一下!PDF中的圖片分為兩種,一種是傳統意義上的圖片,可以直接進行抽取;另一種是各種圖形的組合,這種圖片不能夠直接進行抽取。
1、方法一:可以直接從PDF中抽取的圖片
1.1 Maven引入
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.9</version><type>jar</type> </dependency>
1.2 代碼
?
public static List<String> extractImage(String pdfPath,Integer pdfPage,String picPath) {FileInputStream fis = null;PDDocument document = null;List<String> imageUrls = new ArrayList<>();try {File file = new File( picPath);if (!file.exists()) {file.mkdirs();}// 打開pdf文件流fis = new FileInputStream(pdfPath);// 加載 pdf 文檔,獲取PDDocument文檔對象document = PDDocument.load(fis);PDPage page = document.getPage(pdfPage);PDResources resources = page.getResources();Iterable<COSName> xObjectNames = resources.getXObjectNames();int i = 1;if (xObjectNames != null){Iterator<COSName> iterator = xObjectNames.iterator();while (iterator.hasNext()){COSName key = iterator.next();if (resources.isImageXObject(key)){PDImageXObject image = (PDImageXObject) resources.getXObject(key);BufferedImage bImage = image.getImage();String imageUrl = picPath + File.separator + pdfPage + "-" + i + "."+ image.getSuffix();ImageIO.write(bImage, image.getSuffix(), new File(imageUrl));imageUrls.add(imageUrl);}i++;}}fis.close();document.close();} catch (Exception e) {e.printStackTrace();System.out.println("有異常圖片");}return imageUrls;}
2、方法二:圖形的組合,截圖
2.1 Maven引入
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.17</version> </dependency>
2.2 代碼?
/*** 將PDF文檔拆分成多張圖片,并返回所有圖片的路徑** @param pdfPath* @param pictureFolderPath* @return* @throws Exception*/public static List<String> pdfSwitchToPicture(String pdfPath, String pictureFolderPath,Integer startPage,Integer endPage) {List<String> picUrlList = new ArrayList<>();File file = new File(pictureFolderPath);if (!file.exists()) {file.mkdirs();}try {List<byte[]> imageList = handlePdf(pdfPath,startPage,endPage);AtomicInteger pictureNameNumber = new AtomicInteger(startPage);for (byte[] image : imageList) {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();byteArrayOutputStream.write(image);String pictureUrl = file.getAbsolutePath() + File.separator + pictureNameNumber.getAndIncrement() + ".jpg";byteArrayOutputStream.writeTo(new FileOutputStream(pictureUrl));picUrlList.add(pictureUrl);byteArrayOutputStream.close();}} catch (Exception e) {throw new RuntimeException(e);}return picUrlList;}/*** 處理PDF文檔** @param pdfPath* @return* @throws Exception*/public static List<byte[]> handlePdf(String pdfPath,Integer startPage,Integer endPage) throws Exception {File pdfFile = new File(pdfPath);//加載PDF文檔PDDocument pdDocument = PDDocument.load(pdfFile);//創建PDF渲染器PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);int pageNum = endPage!=null ? endPage : pdDocument.getNumberOfPages();List<byte[]> list = new ArrayList<>();for (int i = startPage-1; i < pageNum; i++) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();//將PDF的每一頁渲染成一張圖片BufferedImage image = pdfRenderer.renderImage(i);ImageIO.write(image, "jpg", outputStream);list.add(outputStream.toByteArray());outputStream.close();}pdDocument.close();return list;}