【问题标题】:How to open bookmarks automatically in Jasper Report PDF export?如何在 Jasper Report PDF 导出中自动打开书签?
【发布时间】:2017-12-13 04:02:05
【问题描述】:

从 Crystal Reports 导出 PDF 时,默认情况下会在打开 PDF 时显示书签面板;但是,在使用 JasperReports 时,书签面板默认不打开,必须手动打开。

JasperReports 如何导出打开时默认显示书签的 PDF?

【问题讨论】:

  • 我不同意对这个问题的密切投票,它不是太宽泛(见答案),它不是调试我的代码问题,也没有记录在 jasper-report 因此它不需要mcve(如果没有记录会怎样?)。

标签: java jasper-reports itext bookmarks export-to-pdf


【解决方案1】:

AFIK 在 jasper-report 中没有 configuration 来设置视图首选项(页面模式)。我唯一的解决方案是用 itext 发布详细的 pdf(用于导出到 pdf 的库,已经在类路径中)

示例

我们将 jasper 作为 PDF 导出到内存流 (ByteArrayOutputStream),然后使用 itext 的 PdfStamper 添加查看器首选项 PageModeUseOutlines1

//Get the JasperPrint object (exact code to achieve this intentional left out since command depends on application)
JasperPrint jasperPrint = JasperFillManager.fillReport(...); 

//Export to pdf into a memory stream
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(memoryStream));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
        
//Use stamper to set viewer prederence 
PdfReader pdfReader = new PdfReader(new ByteArrayInputStream(memoryStream.toByteArray()));
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("my.pdf"));          
pdfStamper.getWriter().setViewerPreferences(PdfWriter.PageModeUseOutlines);
pdfStamper.close();
pdfReader.close();

1.链接指向 itext5 api,但请注意,jasper-reports 实际上使用了 itext 2.1.7 的特殊版本,请参阅 maven 依赖项了解更多信息

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多