You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PDFBox生成PDF时出现超长报错且耗时久的问题求助

Java PDF生成问题求助

用Java开发PDF生成功能,代码已基本完成,但运行程序时编译器输出超长错误信息,且PDF生成耗时约30秒。

相关代码如下:

public void CreatePDF(){
    try {
        // 创建新文档并添加页面
        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);

        // 打开文件选择对话框
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Speichern Sie die PDF-Datei");

        // 显示对话框并等待用户选择
        int userSelection = fileChooser.showSaveDialog(null);

        if (userSelection == JFileChooser.APPROVE_OPTION) {
            // 用户已选择保存位置
            File fileToSave = fileChooser.getSelectedFile();

            // 创建PDF文档内容
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);

            // 标题
            contentStream.beginText();
            contentStream.newLineAtOffset(100, 750);
            contentStream.showText("Überschrift");
            contentStream.endText();

            // 标题下方文本
            contentStream.beginText();
            contentStream.newLineAtOffset(100, 730);
            contentStream.showText("Mein eigenes Variable: Wert");
            contentStream.endText();

            contentStream.close();

            // 将PDF文档保存到选定位置
            document.save(fileToSave);
            document.close();
        } else {
            System.out.println("Speichern abgebrochen.");
        }
    }  catch (IOException e) {
        System.err.println("IOException aufgetreten:");
        e.printStackTrace();
    }
}

已尝试过相关字体问题的解决方案,但问题仍未解决,特此求助。

内容的提问来源于stack exchange,提问作者Lukas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:14:55