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

这篇文章主要介绍了java实现截取PDF指定页并进行图片格式转换功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

1、引入依赖

org.apache.pdfBoxpdfBox2.0.16org.apache.pdfBoxfontBox2.0.16

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 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中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...