gpt4 book ai didi

java - 如何让 JasperViewer 优先保存 PDF 选项

转载 作者:行者123 更新时间:2023-11-30 10:05:58 24 4
gpt4 key购买 nike

我有一个实现,其中 JasperViewer 用于预览和保存报告。查看器提供的第一个保存格式是 .jrprint,我正在尝试将其更改为 pdf。我正在使用以下方法来帮助我解决这个问题

private static void promotePdfSaveFormat(JasperViewer jasperViewer) {
/* get via reflection (with jOOR) the parameter JasperViewer object's member viewer (of type JRViewer) */
JRViewer viewer = on(jasperViewer).call("viewer").get();
/* get via reflection JRViewer object's member tlbToolBar (of type JRViewerToolbar) */
JRViewerToolbar tlbToolBar = on(viewer).call("tlbToolBar").get();
/* call tlbToolBar's getSaveContributors() */
Arrays.stream(tlbToolBar.getSaveContributors()).forEach(
e -> {
/* Find the one that is of type JRPdfSaveContributor (using instanceof) */
if (e instanceof JRPdfSaveContributor){
/* set JRViewerToolbar object member lastSaveContributor value to the instance of JRPdfSaveContributor */
on(tlbToolBar).call("lastSaveContributor").get();
}
}
);
}

每次在类中创建一个新对象时,如 JasperViewer viewer = new JasperViewer(...),我都会在它之后立即添加 promotePdfSaveFormat(viewer)。当我运行该应用程序时,我在 JRViewer viewer = on(jasperViewer).call("viewer").get();

处收到错误

错误如下

org.joor.ReflectException: java.lang.NoSuchMethodException: No similar method viewer with params [] could be found on type class net.sf.jasperreports.view.JasperViewer.

最佳答案

它们被称为 JRSaveContributor .

你可以自己设置JRViewer.setSaveContributors或者重新排序(删除)的一种简单方法是获取实例化的实例,然后循环检查描述以查看您想要保留/重新排序的内容,然后将它们设置回 JasperViewer

快速示例,首先设置 pdf。

final JRViewer jrv = new JRViewer(report);
JRSaveContributor[] sv = jrv.getSaveContributors();
List<JRSaveContributor> reorded = new ArrayList<JRSaveContributor>();
for (JRSaveContributor s : sv) {
if (s.getDescription().contains(".pdf")){
reorded.add(0,s);
}else{
reorded.add(s);
}
}
jrv.setSaveContributors((JRSaveContributor[]) reorded.toArray(new JRSaveContributor[reorded.size()]));

You may also just like to skip .jrprint? if you are using java8 this code can be improved using streams, I leave that exercise to reader.

结果

enter image description here

关于java - 如何让 JasperViewer 优先保存 PDF 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54947086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com