两个jasper模板生成一个pdf:
public static void runTwoReportToPdfFile(String jasper,String jasper1,String destFile,
Map<String,Object> parameters,Connection conn) throws JRException{
File jasperfile = new File(jasper);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
File jasperfile1 = new File(jasper1);
JasperReport jasperReport1 = (JasperReport) JRLoader.loadObject(jasperfile1);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport1, parameters, conn);
List<JRPrintPage> pages = jasperPrint1.getPages();
for(JRPrintPage page : pages){
jasperPrint.addPage(page);
}
JasperExportManager.exportReportToPdfFile(jasperPrint, destFile);
}
多个jasper模板生成一个pdf(1):
public static void runMultipleReportToPdfFile(String[] jaspers, String destFile,
Map<string object>[] parameters, Connection conn) throws JRException{
File jasperfile = new File(jaspers[0]);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters[0], conn);
JasperReport jasperReport1 = null;
File jasperfile1 = null;
JasperPrint jasperPrint1 = null;
List<jrprintpage> pages = null;
for(int i=1;i<jaspers.length;i++){
jasperfile1 = new File(jaspers[i]);
jasperReport1 = (JasperReport) JRLoader.loadObject(jasperfile1);
jasperPrint1 = JasperFillManager.fillReport(jasperReport1, parameters[i],conn);
pages = jasperPrint1.getPages();
for(JRPrintPage page : pages){
jasperPrint.addPage(page);
}
}
JasperExportManager.exportReportToPdfFile(jasperPrint, destFile);
}
多个jasper模板生成一个pdf(2):
public static void runMultipleReportToPdfFile(String[] jaspers, String destFile,
Map<string object>[] parameters, JRDataSource[] jrDataSources) throws JRException{
File jasperfile = new File(jaspers[0]);
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters[0], jrDataSources[0]);
JasperReport jasperReport1 = null;
File jasperfile1 = null;
JasperPrint jasperPrint1 = null;
List<jrprintpage> pages = null;
for(int i=1;i<jaspers.length;i++){
jasperfile1 = new File(jaspers[i]);
jasperReport1 = (JasperReport) JRLoader.loadObject(jasperfile1);
jasperPrint1 = JasperFillManager.fillReport(jasperReport1, parameters[i],jrDataSources[i]);
pages = jasperPrint1.getPages();
for(JRPrintPage page : pages){
jasperPrint.addPage(page);
}
}
JasperExportManager.exportReportToPdfFile(jasperPrint, destFile);
}
多个pdf合成一个pdf:
public static void mergePdf(String[] pdffiles, String destfile) throws Exception{
Document doc = new Document();
OutputStream out = new FileOutputStream(new File(destfile));
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
for(String pdf : pdffiles){
if(pdf==null || pdf.trim().length()==0){
continue;
}
File file = new File(pdf);
if(file.exists()){
PdfReader reader = new PdfReader(pdf);
int i = 0;
while(i<reader.getNumberOfPages()){
i++;
copy.addPage(copy.getImportedPage(reader, i));
}
}
}
//关闭文档
if(doc!=null && doc.isOpen()){
doc.close();
}
}