成都创新互联网站制作重庆分公司

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

这篇文章给大家分享的是有关如何使用java实现截取PDF指定页并进行图片格式转换功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

10年积累的成都网站设计、网站制作、外贸营销网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有淅川免费网站建设让你可以放心的选择与我们合作。

1、引入依赖

  org.apache.pdfbox  pdfbox  2.0.16  org.apache.pdfbox  fontbox  2.0.16

jar包下载地址:

https://mvnrepository.com/artifact/org.apache.pdfbox/pdfboxhttps://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 pages = splitter.split(document);      ListIterator 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实现截取PDF指定页并进行图片格式转换功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


新闻标题:如何使用java实现截取PDF指定页并进行图片格式转换功能
网页网址:http://cxhlcq.com/article/gpjepp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部