java - 如何在Jasper Report PDF导出中自动打开书签?

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

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

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

最佳答案

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

示例

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

//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 依赖项以获取更多信息

关于java - 如何在Jasper Report PDF导出中自动打开书签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47785279/

相关文章:

java - 计算元音的数量并以元音较多的字符串在前的方式打印它们

java - 未找到 android 项目中的 native 库(UnsatisfiedLinkError)

java - JTable 1点边框不出现在网格线上

java - MakeSignature.signDetached 抛出 IllegalArgumentException

java - 如何从字符串中获取特定字符串(子字符串)

java - java中如何检查一个字符串是否是回文?

jasper-reports - 如何在jasper报告中使用divide()方法?

java - 如何在特定方案中对 ireport 交叉表中的列进行排序

node.js - CoffeeScript、Node.js、MongoDB 和 JasperReports,有可能吗?

pdf - 使用 Itextsharp 裁剪 pdf 的左侧