根据itextpdf包, 进行pdf文件操作

博客介绍了使用itextpdf的相关操作,包括添加itextpdf包和编写工具类,可实现从初始页到末尾页生成新文件的功能,聚焦于信息技术中文件处理相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一丶添加itextpdf包
        <!-- pdf文件操作包 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
二丶编写工具类
public class PdfFileUtil {

    /**
     * 截取pdf第几页到第几页生成新的PDF
     *
     * @param realPath    需进行操作的文件路径,全路径(E:/text.pdf)
     * @param savePath    需保存新的pdf路径, 全路径
     * @param from        初始页
     * @param end         末尾页
     */
    public static void splitPDFFile(String realPath, String savePath, int from, int end) throws Exception {
        Document document;
        PdfCopy copy;
        // 读取文件
        PdfReader reader = new PdfReader(realPath);
        // 获取总页数
        int n = reader.getNumberOfPages();
        if(end == 0){
            end = n;
        }
        ArrayList<String> savepaths = new ArrayList<>();
        savepaths.add(savePath);
        document = new Document(reader.getPageSize(1));
        copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));
        document.open();
        for(int j=from; j<=end; j++) {
            document.newPage();
            PdfImportedPage page = copy.getImportedPage(reader, j);
            copy.addPage(page);
        }
        document.close();
    }
}

end, 这里是选择初始页到末尾页生成新的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值