java操作PDF,有一个很好用的工具——pdfbox。只需要引入依赖,即可使用。

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox-app</artifactId><version>2.0.21</version>
</dependency>

利用这个工具,可以实现很多的功能,我这里示例了以下几种:

  1. 加载PDF文档
  2. 创建一个单页的PDF空文档
  3. 获取PDF文档总页数
  4. 获取pdf文档的所有分页对象
  5. 给整个PDF文件分页,形成多个pdf单页文件
  6. 合并多个单页PDF文件,输出一个合并后的PDF文档
  7. 图片转PDF
  8. 获取pdf单页分辨率

代码如下:

  /*** 从文件中加载pdf** @param file 文件* @return* @throws IOException*/public static PDDocument load(File file) throws IOException {if (!file.exists() || file.isDirectory()) {return null;}return PDDocument.load(file);}/*** 从文件流中加载pdf** @param inputStream 文件输入流* @return* @throws IOException*/public static PDDocument load(InputStream inputStream) throws IOException {if (inputStream == null || inputStream.available() == 0) {return null;}return PDDocument.load(inputStream);}/*** 创建一个单页的PDF空文档** @param outputFile* @return* @throws IOException*/public static PDDocument getBlankPDF(File outputFile) throws IOException {//首先创建pdf文档类PDDocument pdf = null;pdf = new PDDocument();//实例化pdf页对象PDPage blankPage = new PDPage();//插入文档类pdf.addPage(blankPage);//保存pdf.save(outputFile);return pdf;}/*** 获取pdf总页数** @param pdf* @return*/public static int pageCount(PDDocument pdf) {return pdf.getNumberOfPages();}/*** 获取pdf文档的所有分页对象** @param pdf* @return 返回的list集合*/public static List<PDPage> getPageList(PDDocument pdf) {int count = pageCount(pdf);List<PDPage> pages = new ArrayList<>(64);PDPageTree pdPages = pdf.getPages();for (int i = 0; i < count; i++) {PDPage pdPage = pdPages.get(i);pages.add(pdPage);}return pages;}/*** 给整个PDF文件分页,形成多个pdf单页文件** @param inputStream  pdf文件流* @param outputParent 输出文件的父目录* @throws IOException*/public static Integer pageSpilt(InputStream inputStream, File outputParent) throws IOException {if (!outputParent.exists() || !outputParent.isDirectory()) {throw new RuntimeException("输出文件的父目录不存在");}PDDocument pdf = load(inputStream);try {int numberOfPages = pageCount(pdf);for (int i = 0; i < numberOfPages; i++) {PDDocument document = new PDDocument();document.addPage(pdf.getPage(i));document.save(new File(outputParent, i + 1 + ".pdf"));close(document);}return numberOfPages;} finally {close(pdf);close(inputStream);}}/*** 合并多个单页PDF文件,输出一个合并后的PDF文档** @param inputParent* @param outputFile* @param sortor* @throws IOException*/public static void combine(File inputParent, String outputFile, FileSortor sortor) throws IOException {if (!inputParent.exists() || !inputParent.isDirectory()) {throw new RuntimeException("输入文件的父目录不存在");}if (new File(outputFile).exists()) {throw new RuntimeException("输出文件已存在");}File[] files = inputParent.listFiles();if (sortor != null) {sortor.sort(files);}PDFMergerUtility merger = new PDFMergerUtility();//输出目标路径merger.setDestinationFileName(outputFile);for (int i = 0; i < files.length; i++) {if (files[i].getName().toLowerCase().endsWith(".pdf")) {merger.addSource(files[i]);}}merger.mergeDocuments(null);}/*** 获取pdf单页分辨率** @param page* @return*/public static String getResolution(PDPage page) {PDRectangle rectangle = page.getArtBox();double width = Math.ceil(rectangle.getWidth());double height = Math.ceil(rectangle.getHeight());return (int) width + "*" + (int) height;}/*** 图片转PDF** @param inputFile  图片路径* @param outputFile 生成pdf的文件路径* @throws IOException*/public static void convertImgToPDF(String inputFile, String outputFile) throws IOException {if (!new File(inputFile).exists()) {throw new RuntimeException("输入文件不存在");}if (!outputFile.toLowerCase().endsWith(".pdf")) {throw new RuntimeException("只能转成pdf文件");}PDDocument document = new PDDocument();InputStream inputStream = new FileInputStream(inputFile);BufferedImage bimg = ImageIO.read(inputStream);float width = bimg.getWidth();float height = bimg.getHeight();PDPage page = new PDPage(new PDRectangle(width, height));document.addPage(page);PDImageXObject img = PDImageXObject.createFromFile(inputFile, document);PDPageContentStream contentStream = new PDPageContentStream(document, page);contentStream.drawImage(img, 0, 0, width, height);contentStream.close();close(inputStream);document.save(outputFile);close(document);}public static void close(InputStream inputStream) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}public static void close(PDDocument pdf) {try {pdf.close();} catch (IOException e) {e.printStackTrace();}}/*** 文件排序器*/public interface FileSortor {/*** 源文件组** @param sources*/void sort(File[] sources);}

java操作PDF文件,可支持分页、合并、图片转PDF等相关推荐

  1. python图片转pdf文件_用python 制作图片转pdf工具

    这篇文章主要介绍了用python 制作图片转pdf工具的思路及代码,非常详细,有需要的小伙伴参考下 最近因为想要看漫画,无奈下载的漫画是jpg的格式,网上的转换器还没一个好用的,于是乎就打算用pyth ...

  2. java操作excel文件基础架构实现,支持2007以上版本

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/22800915 java操作office文件现在主流的jar包有jxl和POI,由 ...

  3. JAVA操作xml文件

    参考:java操作xml方法,总有适合你的 XML技术在Java开发中的应用 1.简介 xml: extensible Markup Language 1.1使用场景: 不同的系统之间交互时使用xml ...

  4. Java实现给PDF文件加文字水印和图片水印(可以自定义水印格式)

    使用Java代码给PDF文件加文字水印 直接上代码运行即可 依赖 有的可能用不上我直接复制全部了 <dependencies><!--word文件转PDF以及水印-->< ...

  5. java+icepdf+下载_Java使用icepdf将pdf文件按页转成图片

    本文实例为大家分享了Java使用icepdf将pdf文件按页转成图片的具体代码,供大家参考,具体内容如下 Maven icepdf包,这里过滤掉jai-core org.icepdf.os icepd ...

  6. Java操作word文件的工具选择

    Java操作word文件的工具选择 使用Java语言,创建doc.docx.excel.pdf等文档,并对文档进行一系列操作. Spire.Doc for Java https://blog.csdn ...

  7. 通过itextpdf操作PDF,动态向PDF文件最后一页添加图片

    通过itextpdf操作PDF,向PDF文件最后一页添加图片(缩放图片并判断最后一页是否能放下图片) 本人第一篇博客,哈哈!第一次接触itextpdf,想实现将图片向PDF尾部追加(判断原页面使用情况 ...

  8. Java输出PPT文件(一) - 合并PPT

    Java输出PPT文件(一) - 合并PPT 文章目录 Java输出PPT文件(一) - 合并PPT 0. 前言 1. 依赖 2. 代码 3. 测试 3.1 模板准备 3.2 合并结果 4. 问题 4 ...

  9. 高级知识点:excel4j实现java操作excel文件的读写

    介绍 java操作excel文件最经典的是POI,但是其api 较多,代码量巨大,反复重复 excel4j ap则简洁太多,利用pojo建模,利用注解进行标识@ExcelField(title = & ...

  10. shell换行合并多个文件_如何合并多个pdf文件?这里有合并PDF最简单的方法

    如何合并多个pdf文件?今天在整理一些PDF文件的时候,由于文件的数量比较多,我就想着将这些PDF文件合并起来,好在平时也了解了一些PDF合并的方法,很快就完成了文件的合并.想到应该还有很多朋友还不知 ...

最新文章

  1. python在审计中的应用-基于python的自动化代码审计
  2. Python编程基础:第五十五节 map函数Map
  3. 使用ViewPager制作Android引导界面
  4. 从Tom说JSP原理
  5. 丰胸神器?网友曝椰树椰汁新广告低俗 疑似虚假宣传被调查...
  6. Bzoj1034 [ZJOI2008]泡泡堂BNB
  7. Linux 下超级有趣的命令
  8. 网络安全--安全攻防概述
  9. Edge浏览器启用ie模式,并且打开调试面板
  10. 手把手教你玩转KVM虚拟机--KVM管理虚拟机
  11. 来来来!docker清华源
  12. 前沿关注 | 5G和边缘计算将如何改变AR和VR?
  13. 回归算法———逻辑回归
  14. Faker的妙用---造数
  15. c++如何保留两位小数
  16. 设备VMnet0上的网桥因桥接的以太网接口关闭而暂时停止运行(此虚拟机可能无法与主机或网络中的其他计算机通信)
  17. 浙江理工c语言复试试题,2016年浙江理工大学信息学院C语言程序设计复试笔试最后押题五套卷...
  18. python和matlab读取SST数据(海洋的温度)(.nc文件)并绘图
  19. python身体指数BMI
  20. 请移驾ray7hu.com

热门文章

  1. 学习数据结构的一些预备知识
  2. windbg-!cs、~~[TID](经典死锁)
  3. 1688/阿里巴巴/拼多多API接口
  4. .mk 文件中,= 、:= 、?= 、+= 的区别
  5. luogu2763 试题库问题
  6. APSIM实战练习:Kingsthorpe土壤水分蒸发研究
  7. 面向智能制造全价值链的精益数字孪生体
  8. git pull报错unexpected disconnect while reading sideband packet
  9. 司徒正美写给前端开发者的算法书(文末抽奖送书)
  10. 都来中大奖啦~双色球随机算法!