Java处理PDF文件

该博客介绍了如何使用Apache PDFBox库进行PDF文档的操作,包括创建带有中文文本的PDF,读取PDF内容以及合并多个PDF文件。在创建PDF时,需要导入外部字体以支持中文。读取时,通过PDFTextStripper获取文本内容。合并PDF则利用PDFMergerUtility将多个源文件整合成一个PDF。
摘要由CSDN通过智能技术生成

Apache PDFBox

  • 主要功能:创建、提取文本,分割 / 合并 / 删除 …
  • 主要类:
  • PDDocument PDF文档对象
  • PDFTextStripper PDF文本对象
  • PDFMergerUtility 合并工具

写入PDF文件

public static void main(String[] args) {
    createPDFFile();
}
@SuppressWarnings("all")
public static void createPDFFile() {
    PDDocument doc = null;
    PDPage page = null;

    try {
        doc = new PDDocument();
        page = new PDPage(PDRectangle.A4);
        // 导入外部字体
        PDFont font =  PDType0Font.load(doc, new File("D:\\PingFang Bold.ttf"));
        PDPageContentStream content = new PDPageContentStream(doc, page);
        page.getResources().add(font);
        // 给PDF添加一页
        doc.addPage(page);
        // 设置字体
        content.setFont(font, 12);
        // 开始设置文本
        content.beginText();
        content.moveTextPositionByAmount(100, 700);
        content.showText("你好,世界");

        // 结束设置文本
        content.endText();
        content.close();
        // PDF保存
        doc.save("test.pdf");
        doc.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

注意: 当写入中文时需要导入外部字体包,否则会报U+4F60 (’.notdef’) is not available in this font Helvetica-Bold encoding: WinAnsiEncoding 错误

读取PDF文件

public static void main(String[] args) {
    readPDFFile();
}

public static void readPDFFile() {
    File pdfFile = new File("test.pdf");
    try {
        PDDocument doc = PDDocument.load(pdfFile);
        // 判断是否有权限访问文本
        AccessPermission permission = doc.getCurrentAccessPermission();
        boolean notAccess = ! permission.canExtractContent();
        if (notAccess) {
            throw new IOException("无权限访问");
        }

        // 获取页码
        int pages = doc.getNumberOfPages();
        // 读取文本内容
        PDFTextStripper stripper = new PDFTextStripper();
        // 设置按顺序输出
        stripper.setSortByPosition(true);
        // 起始页
        stripper.setStartPage(1);
        stripper.setEndPage(pages);
        String content = stripper.getText(doc);
        System.out.println(content);

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

合并PDF

public static void main(String[] args) throws Exception {
    merge();
}
public static void merge() throws Exception {
    FileOutputStream fos =
            new FileOutputStream(
            new File("merge.pdf"));
    ByteArrayOutputStream merge = null;
    File file1 = new File("E:\\TangJiachang\\java学习\\MavenDemo\\test.pdf");
    File file2 = new File("E:\\TangJiachang\\java学习\\MavenDemo\\test1.pdf");

    List<InputStream> list = new ArrayList<>();
    try {
        list.add(new FileInputStream(file1));
        list.add(new FileInputStream(file2));
        merge = new ByteArrayOutputStream();

        // 设置来源和目标
        PDFMergerUtility pdfMerger = new PDFMergerUtility();
        pdfMerger.addSources(list);
        pdfMerger.setDestinationStream(merge);

        // 设置合并选项
        PDDocumentInformation pdfDocumentInfo = new PDDocumentInformation();
        pdfMerger.setDestinationDocumentInformation(pdfDocumentInfo);

        // 合并
        pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

        fos.write(merge.toByteArray());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值