Jasper报告 – 在PDF文档中设置作者属性

有没有办法通过在从Java调用Jasper时设置参数来将Author属性设置为PDF文档。

在此处输入图像描述

这就是我从Java生成Jasper报告的方法。

JasperPrint jasperPrint; String outFile = "39285923953222.pdf"; HashMap hm = new HashMap(); hm.put("ID",id); hm.put("FOOTER",Constants.FOOTER); // Set somehow a string for the author name Session session = this.sessionFactory.openSession(); Connection con = session.connection(); jasperPrint = JasperFillManager.fillReport(jasperPath + "myReport.jasper", hm, con); JasperExportManager.exportReportToPdfFile(jasperPrint, outPath + outFile); 

查看JRPdfExporterParameter中的静态字段METADATA_AUTHOR
使用JRPdfExporter而不是JasperExportManager

示例:

 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath + "myReport.jasper", hm, con); JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE, outPath + outFile); exporter.setParameter(JRPdfExporterParameter.METADATA_AUTHOR, "Adnan"); exporter.setParameter(JRPdfExporterParameter.METADATA_TITLE, "Title"); // ... exporter.exportReport(); 

不确定这是否是正确的方法,但你可能想看看jasperPrint.getPropertyNames()jasperPrint.getPropertiesMap() ,看看你是否有任何作者属性。

根据这篇文章, JRExporter在5.6中被弃用了。 我这样做是为了工作:

  ... final JRPdfExporter exporter = new JRPdfExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); final SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration(); configuration.setMetadataTitle(title); configuration.setMetadataAuthor(author); configuration.setMetadataCreator(creator); configuration.setMetadataSubject(subject); configuration.setMetadataKeywords(keywords); ...