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

本文展示了如何使用Java进行PDF分页读取和分页拆分。首先介绍了在Maven中添加Apache PDFBox库来实现分页读取PDF内容,并提供了详细的代码示例。然后,通过引入iTextPDF库,讲解了如何将PDF文件按页拆分成多个子文件。文章还提供了相关库的官网链接供进一步学习。
摘要由CSDN通过智能技术生成

本文以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 代码

//String fileName: PDF路径;
//int from:要读取得PDF开始页码;
//int end:要读取得PDF结束页码;
//每页之间分割字符串:“PDF解析第\d+页”
public static String readPdfByPage(String fileName, int from, int end) {
        String result = "";
        File file = new File(fileName);
        FileInputStream in = null;
        try {
            in = new FileInputStream(fileName);
            // 新建一个PDF解析器对象
            PDFParser parser = new PDFParser(new RandomAccessFile(file,"rw"));
            // 对PDF文件进行解析
            parser.parse();
            // 获取解析后得到的PDF文档对象
            PDDocument pdfdocument = parser.getPDDocument();
            int size = pdfdocument.getNumberOfPages();
            // 新建一个PDF文本剥离器
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(false); //sort:设置为true则按照行进行读取,默认是false
            //一页一页读取
            for(int i = from ; i <= end;i++ ){
               // 设置起始页
                stripper.setStartPage(i);
               // 设置结束页
                stripper.setEndPage(i);
                // 从PDF文档对象中剥离文本
                String pageStr = stripper.getText(pdfdocument);
                result = result + pageStr + "\n" + "PDF解析第"+ i + "页\n";
            }

                //一次读取完
                // 设置起始页
                // stripper.setStartPage(from);
               // 设置结束页
               //  stripper.setEndPage(end);
                // 从PDF文档对象中剥离文本
               //  String result = stripper.getText(pdfdocument);

        } catch (Exception e) {
            System.out.println("读取PDF文件" + file.getAbsolutePath() + "生失败!" + e);
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
        return result;
    }

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 代码

public static void pdfToSub(String filePath,String newFile, int from, int end) {
        Document document = null;
        PdfCopy copy = null;
        try {
            PdfReader reader = new PdfReader(filePath);
            //总页数
            int n = reader.getNumberOfPages();
            if (end == 0) {
                end = n;
            }
            document = new Document(reader.getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newFile));
            document.open();
            for (int j = from; j <= end; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2.2.2 分页效果

不再赘述!

2.2.3 itextpdf官网

https://itextpdf.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值