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

OpenPDF PDF生成布局问题求助及代码示例

OpenPDF PDF生成布局问题求助及代码示例

各位好呀,最近用OpenPDF生成PDF时遇到了布局适配的问题,折腾半天没搞定,把我的代码贴出来,麻烦懂的大佬帮忙看看哪里出问题了🥺:

@Override   
public void generateReport() {     
    try (final FileOutputStream fos = new FileOutputStream("report_example.pdf")) {       
        final Image img = Image.getInstance(header.getContentAsByteArray());       
        img.scaleToFit(600, 200);       
        img.setAlignment(ALIGN_CENTER);        

        final Document document = new Document(           
            A4,           
            36,           
            36,           
            0,           
            36);        

        // 原代码此处存在输入错误,我按常规写法补全了参数
        final PdfWriter writer = PdfWriter.getInstance(document, fos);
        document.open();
        document.add(img);
        // 这里本来还有后续的文档内容生成逻辑,但目前卡在布局上了
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

现在的问题是图片的居中效果不对,还有文档的边距设置好像没起到预期作用,有没有大佬能给点调整建议呀?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 12:35:31