iReport PDF合并示例

两个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();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值