代码之家  ›  专栏  ›  技术社区  ›  Ankur.S

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

  •  2
  • Ankur.S  · 技术社区  · 8 年前

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

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

    1 回复  |  直到 8 年前
        1
  •  2
  •   Community Mohan Dere    6 年前

    AFIK没有 configuration 在jasper report中设置视图首选项(页面模式)。我唯一的解决方案是用itext(用于导出为pdf的库,已经在classpath中)发布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();
    

    链接到itext5 api,但请注意jasper reports实际上使用了itext 2.1.7的一个特殊版本请参见maven dependence以获取更多信息

    推荐文章