Java提取/拆分/合并PDF工具类

1. 引言

在日常工作中,我们经常需要处理 PDF 文件,包括合并、拆分、提取页面等操作。为了简化这些操作,提高开发效率,我们可以使用一个常用的 PDF 操作工具类。本文将介绍一个常用的 Java PDF 操作工具类,并提供详细的使用说明和示例。

2. 工具类介绍

工具类的名称:PdfUtil

工具类的功能:提供常用的 PDF 操作方法,包括合并、拆分、提取页面等。

3. 工具类示例及使用说明

以下是 PdfUtil 工具类的代码示例:

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * PDF 操作工具类,提供常用的 PDF 操作方法
 */
public class PdfUtil {

    /**
     * 合并多个 PDF 文件为一个文件
     *
     * @param sourcePaths    待合并的 PDF 文件路径列表
     * @param destinationPath 合并后的 PDF 文件路径
     * @throws IOException       IO 异常
     * @throws DocumentException PDF 文档异常
     */
    public static void mergePdfFiles(List<String> sourcePaths, String destinationPath)
            throws IOException, DocumentException {
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(destinationPath));
        document.open();

        for (String sourcePath : sourcePaths) {
            PdfReader reader = new PdfReader(sourcePath);
            int numPages = reader.getNumberOfPages();
            for (int i = 1; i <= numPages; i++) {
                copy.addPage(copy.getImportedPage(reader, i));
            }
            reader.close();
        }

        document.close();
    }

    /**
     * 拆分 PDF 文件为多个文件
     *
     * @param sourcePath       待拆分的 PDF 文件路径
     * @param destinationPaths 拆分后的 PDF 文件路径列表
     * @throws IOException       IO 异常
     * @throws DocumentException PDF 文档异常
     */
    public static void splitPdfFile(String sourcePath, List<String> destinationPaths)
            throws IOException, DocumentException {
        PdfReader reader = new PdfReader(sourcePath);

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            Document document = new Document();
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(destinationPaths.get(i - 1)));
            document.open();
            copy.addPage(copy.getImportedPage(reader, i));
            document.close();
        }

        reader.close();
    }

    /**
     * 提取 PDF 文件的指定页面为新文件
     *
     * @param sourcePath      待提取页面的 PDF 文件路径
     * @param destinationPath 提取后的页面文件路径
     * @param pageNumber     要提取的页面页码(从1开始)
     * @throws IOException       IO 异常
     * @throws DocumentException PDF 文档异常
     */
    public static void extractPageFromPdf(String sourcePath, String destinationPath, int pageNumber)
            throws IOException, DocumentException {
        PdfReader reader = new PdfReader(sourcePath);
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(destinationPath));
        document.open();
        copy.addPage(copy.getImportedPage(reader, pageNumber));
        document.close();
        reader.close();
    }

    // TODO: 添加更多的 PDF 操作方法

}

4. 结论

通过使用上述的 PdfUtil 工具类,我们可以方便地进行常见的 PDF 操作,包括合并、拆分、提取页面等操作,从而提高开发效率。通过合理的使用示例和详细的注释,开发人员可以快速掌握该工具类的使用方法,并在实际项目中应用它。

以下是 PdfUtil 工具类的使用示例:

import com.lowagie.text.DocumentException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        try {
            List<String> sourcePaths = new ArrayList<>();
            sourcePaths.add("path/to/source1.pdf");
            sourcePaths.add("path/to/source2.pdf");
            String mergedFilePath = "path/to/merged.pdf";

            // 合并多个 PDF 文件示例
            PdfUtil.mergePdfFiles(sourcePaths, mergedFilePath);
            System.out.println("PDF 文件合并完成,合并后的文件路径:" + mergedFilePath);

            // 拆分 PDF 文件示例
            String sourceFilePath = "path/to/source.pdf";
            List<String> splitFilePaths = new ArrayList<>();
            splitFilePaths.add("path/to/split1.pdf");
            splitFilePaths.add("path/to/split2.pdf");
            PdfUtil.splitPdfFile(sourceFilePath, splitFilePaths);
            System.out.println("PDF 文件拆分完成,拆分后的文件路径:" + splitFilePaths);

            // 提取 PDF 页面示例
            String sourceFile = "path/to/source.pdf";
            int pageNumber = 2;
            String extractedPageFile = "path/to/extractedPage.pdf";
            PdfUtil.extractPageFromPdf(sourceFile, extractedPageFile, pageNumber);
            System.out.println("PDF 页面提取完成,提取后的页面文件路径:" + extractedPageFile);

        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
    }
}

以上是关于常用的 PDF 操作工具类的详细介绍及使用示例。希望本文对你在日常工作中处理 PDF 文件的需求有所帮助。如果你有任何问题或建议,欢迎在下方留言。

参考链接:

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值