【问题标题】:Block copy PDF document块复制 PDF 文档
【发布时间】:2023-03-31 17:51:01
【问题描述】:

我想使用 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();

谁能帮我解决这个问题?

【问题讨论】:

    标签: java pdf jasper-reports


    【解决方案1】:

    你应该在使用 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 已弃用

    【讨论】:

      【解决方案2】:

      可能我错了,但我坚信这是不可能的。您正在构建 PDF 文档并使用 PDF Reader 进行审阅。复制部分文档的能力是阅读器的功能,因此您无法在文档构建阶段进行控制。 PDF 不是 HTML,您可以自己处理事件并因此阻止其中一些事件。

      我会很高兴知道我错了......

      【讨论】:

        猜你喜欢
        • 2017-11-18
        • 2018-06-23
        • 2018-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-07
        • 1970-01-01
        相关资源
        最近更新 更多