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

时间:2017-12-13 04:02:06

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

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

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

1 个答案:

答案 0 :(得分:2)

AFIK在jasper-report中没有configuration来设置视图首选项(页面模式)。我唯一的解决方案是使用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依赖以获取更多信息