欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java实现截取PDF指定页并进行图片格式转换功能

时间:2021-01-10 11:07:20|栏目:JAVA代码|点击:

1、引入依赖

<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>pdfbox</artifactId>
  <version>2.0.16</version>
</dependency>
<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>fontbox</artifactId>
  <version>2.0.16</version>
</dependency>

jar包下载地址:

https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox

2、实现DEMO

package com.dddpeter.app;
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.ListIterator;
public class PDFUtils {
  public static String splitPdf(int pageNum, String source, String dest) {
    File indexFile = new File(source);
    File outFile = new File(dest);
    PDDocument document = null;
    try {
      document = PDDocument.load(indexFile);
      // document.getNumberOfPages();
      Splitter splitter = new Splitter();
      splitter.setStartPage(pageNum);
      splitter.setEndPage(pageNum);
      List<PDDocument> pages = splitter.split(document);
      ListIterator<PDDocument> iterator = pages.listIterator();
      while (iterator.hasNext()) {
        PDDocument pd = iterator.next();
        if (outFile.exists()) {
          outFile.delete();
        }
        pd.save(outFile);
        pd.close();
        if (outFile.exists()) {
          return outFile.getPath();
        }
      }
      document.close();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  public static void pdfFileToImage(File pdffile,String targetPath){
    try {
      FileInputStream instream = new FileInputStream(pdffile);
      InputStream byteInputStream=null;
      try {
        PDDocument doc = PDDocument.load(instream);
        PDFRenderer renderer = new PDFRenderer(doc);
        int pageCount = doc.getNumberOfPages();
        if (pageCount > 0) {
          BufferedImage image = renderer.renderImage(0, 4.0f);
          image.flush();
          ByteArrayOutputStream bs = new ByteArrayOutputStream();
          ImageOutputStream imOut;
          imOut = ImageIO.createImageOutputStream(bs);
          ImageIO.write(image, "png", imOut);
          byteInputStream = new ByteArrayInputStream(bs.toByteArray());
          byteInputStream.close();
        }
        doc.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      File uploadFile = new File(targetPath);
      FileOutputStream fops;
      fops = new FileOutputStream(uploadFile);
      fops.write(readInputStream(byteInputStream));
      fops.flush();
      fops.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static byte[] readInputStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
      outStream.write(buffer, 0, len);
    }
    inStream.close();
    return outStream.toByteArray();
  }
  public static void main(String[] args) {
    String path = splitPdf(4,"D:\\data\\11.pdf","D:\\data\\out11.pdf");
    File file =new File(path);
    //上传的是png格式的图片结尾
    String targetfile="D:\\data\\out11.png";
    pdfFileToImage(file,targetfile);
  }
}

总结

上一篇:Java中自定义异常详解及实例代码

栏    目:JAVA代码

下一篇:java socket 详细介绍

本文标题:java实现截取PDF指定页并进行图片格式转换功能

本文地址:http://www.codeinn.net/misctech/43330.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有