当前位置:   article > 正文

Java的PDF分页操作:分页读取、分页拆分_java pdf分页

java pdf分页

本文以Java示例展示读取PDF分页读取、分页拆分的方法。

1、分页读取

1.1 Maven仓库下载导入

在pom.xml中配置maven路径,指定依赖,如下:

   <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.15</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/jempbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>jempbox</artifactId>
            <version>1.8.16</version>
        </dependency>

 

1.2 读取

1.2.1 代码

  1. //String fileName: PDF路径;
  2. //int from:要读取得PDF开始页码;
  3. //int end:要读取得PDF结束页码;
  4. //每页之间分割字符串:“PDF解析第\d+页”
  5. public static String readPdfByPage(String fileName, int from, int end) {
  6. String result = "";
  7. File file = new File(fileName);
  8. FileInputStream in = null;
  9. try {
  10. in = new FileInputStream(fileName);
  11. // 新建一个PDF解析器对象
  12. PDFParser parser = new PDFParser(new RandomAccessFile(file,"rw"));
  13. // 对PDF文件进行解析
  14. parser.parse();
  15. // 获取解析后得到的PDF文档对象
  16. PDDocument pdfdocument = parser.getPDDocument();
  17. int size = pdfdocument.getNumberOfPages();
  18. // 新建一个PDF文本剥离器
  19. PDFTextStripper stripper = new PDFTextStripper();
  20. stripper.setSortByPosition(false); //sort:设置为true则按照行进行读取,默认是false
  21. //一页一页读取
  22. for(int i = from ; i <= end;i++ ){
  23. // 设置起始页
  24. stripper.setStartPage(i);
  25. // 设置结束页
  26. stripper.setEndPage(i);
  27. // 从PDF文档对象中剥离文本
  28. String pageStr = stripper.getText(pdfdocument);
  29. result = result + pageStr + "\n" + "PDF解析第"+ i + "页\n";
  30. }
  31. //一次读取完
  32. // 设置起始页
  33. // stripper.setStartPage(from);
  34. // 设置结束页
  35. // stripper.setEndPage(end);
  36. // 从PDF文档对象中剥离文本
  37. // String result = stripper.getText(pdfdocument);
  38. } catch (Exception e) {
  39. System.out.println("读取PDF文件" + file.getAbsolutePath() + "生失败!" + e);
  40. e.printStackTrace();
  41. } finally {
  42. if (in != null) {
  43. try {
  44. in.close();
  45. } catch (IOException e1) {
  46. }
  47. }
  48. }
  49. return result;
  50. }

1.2.2 读取效果

 2、分页拆分

2.1 Maven仓库下载导入

在pom.xml中配置maven路径,指定依赖,如下:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.1</version>
    <type>jar</type>
</dependency>

2.2 读取

2.2.1 代码

  1. public static void pdfToSub(String filePath,String newFile, int from, int end) {
  2. Document document = null;
  3. PdfCopy copy = null;
  4. try {
  5. PdfReader reader = new PdfReader(filePath);
  6. //总页数
  7. int n = reader.getNumberOfPages();
  8. if (end == 0) {
  9. end = n;
  10. }
  11. document = new Document(reader.getPageSize(1));
  12. copy = new PdfCopy(document, new FileOutputStream(newFile));
  13. document.open();
  14. for (int j = from; j <= end; j++) {
  15. document.newPage();
  16. PdfImportedPage page = copy.getImportedPage(reader, j);
  17. copy.addPage(page);
  18. }
  19. document.close();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }

2.2.2 分页效果

不再赘述!

2.2.3 itextpdf官网

https://itextpdf.com

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/530673
推荐阅读
相关标签
  

闽ICP备14008679号