从 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/