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

如何在RPGLE中通过iText修改PdfPTable宽度?

解决RPGLE调用iText setWidthPercentage时的NoSuchMethodError问题

你遇到的java.lang.NoSuchMethodError主要是两个问题导致的:一是RPGLE过程定义的参数类型和Java方法不匹配,二是缺少实例方法必需的this参数传递。

旧版iText(com.lowagie.text.pdf.PdfPTable)的setWidthPercentage核心重载方法接受float类型参数,且作为Java实例方法,调用时必须传递当前表格对象实例作为第一个参数——这两点你之前的定义都没做到,所以JVM找不到匹配的方法签名。

正确的RPGLE过程定义

修改参数类型并补上this参数:

dcl-pr  pTable_setWidthPercentage  ExtProc(*JAVA
                                   :'com.lowagie.text.pdf.PdfPTable'
                                   :'setWidthPercentage');
   this                          object(*JAVA:'com.lowagie.text.pdf.PdfPTable') const;
   prWidthPct                    float(4)   value;
end-pr;

调用示例

创建表格后直接调用该过程:

newTable = new_pdfptable(2);
// 设置表格宽度为页面的90%
pTable_setWidthPercentage(newTable : 90.0);

如果需要更精细的宽度控制(比如92.5%),直接传入带小数的float值即可。

备选方案:设置绝对宽度

如果仍有问题,可尝试通过setTotalWidth+setLockedWidth组合设置表格绝对宽度:

// 定义setTotalWidth过程
dcl-pr pTable_setTotalWidth ExtProc(*JAVA
                                  :'com.lowagie.text.pdf.PdfPTable'
                                  :'setTotalWidth');
   this                         object(*JAVA:'com.lowagie.text.pdf.PdfPTable') const;
   widths                       float(4) dim(2) const; // 两列对应两个宽度值
end-pr;

// 定义setLockedWidth过程
dcl-pr pTable_setLockedWidth ExtProc(*JAVA
                                    :'com.lowagie.text.pdf.PdfPTable'
                                    :'setLockedWidth');
   this                           object(*JAVA:'com.lowagie.text.pdf.PdfPTable') const;
   locked                         ind value;
end-pr;

// 调用示例
dcl-s tableWidths float(4) dim(2);
tableWidths(1) = 320.0; // 第一列宽度
tableWidths(2) = 320.0; // 第二列宽度
pTable_setTotalWidth(newTable : tableWidths);
pTable_setLockedWidth(newTable : *ON);

通过这种方式可以精准控制表格总宽度,替代百分比设置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:22:29