我想使用Java进行块复制(ctrl + c ctrl + v)PDF文档。我有一个使用JasperReport构建PDF文档的代码...

//seta o caminho dos arquivos jasper
        String pathLote = ScopeSupport.getServletContext().getRealPath("priv/sgc/relatorios/AtaPregaoLotePageReport.jasper");
        String pathCabecalho = ScopeSupport.getServletContext().getRealPath("priv/sgc/relatorios/CabecalhoPageReport.jasper");
        String pathRodape = ScopeSupport.getServletContext().getRealPath("priv/sgc/relatorios/rodapePageReport.jasper");
        String imagemDir = ScopeSupport.getServletContext().getRealPath("/priv/comum/img");

        //HashMap parametros passa o parametro usado na query e o caminho da imagem
        HashMap<String,Object> parametros = new HashMap<String,Object>();
        parametros.put("idPregao", idPregao);
        parametros.put("idLote", idLote);
        parametros.put("IMAGEM_DIR", imagemDir + "/");
        parametros.put("USUARIO", "NomeUsuario" );
        parametros.put("texto", texto);
        parametros.put("numeroAta", numAta);

        if(numAta != null && numAta > 0)
            parametros.put("relatorio", "Ata "+numAta);

        HashMap<String,Object> parametrosSub = new HashMap<String,Object>();
        parametrosSub.put("CabecalhoPageReport", pathCabecalho);
        parametrosSub.put("rodapePageReport", pathRodape);
        parametrosSub.put("AtaPregaoPorLotePageReport", pathLote);

        for(String element : parametrosSub.keySet()){
            parametros.put(element, (JasperReport) JRLoader.loadObject((String) (parametrosSub.get(element))));
        }

        JasperReport report = (JasperReport) JRLoader.loadObject( pathLote );
        JasperPrint printRel = JasperFillManager.fillReport( report, parametros, getJDBCConnection() );
        byte[] bytes = JasperExportManager.exportReportToPdf(printRel);

        httpResponse.setHeader("Content-Disposition","attachment; filename=\""+ report.getName() + ".pdf" +"\";");
        httpResponse.setContentLength(bytes.length);
        httpResponse.setContentType("application/pdf");

        ServletOutputStream ouputStream = httpResponse.getOutputStream();
        ouputStream.write(bytes, 0, bytes.length);
        ouputStream.flush();
        ouputStream.close();


谁能帮我这个忙?

最佳答案

使用jasper report 5.6.x时应该尝试一下

  SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
  configuration.setEncrypted(true);
  configuration.set128BitKey(true);
  configuration.setUserPassword("jasper");
  configuration.setOwnerPassword("reports");
  configuration.setPermissions(PdfWriter.ALLOW_PRINTING); //PdfWriter.ALLOW_COPY is no set
  exporter.setConfiguration(configuration);
  //extrac of http://jasperreports.sourceforge.net/sample.reference/pdfencrypt/index.html


在旧版中,您可以使用JRExporterParameter

  pdfExporter.setParameter(JRPdfExporterParameter.PERMISSIONS, PdfWriter.ALLOW_PRINTING);
  //when you want copy files use
  //pdfExporter.setParameter(JRPdfExporterParameter.PERMISSIONS, PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY);

  pdfExporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, true);
        pdfExporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, ".sigaAr4gos@");


注意:JRPdfExporterParameter是降级

10-07 22:07