Build a jasper report merging pages with different format

- How to build a report with A4_portrait and Landscape pages ?

Due to the limitation of jasperReport to combine different oriented pages in one report, we are going to use Itext to create this kind of reports.

1 – In Ireport build individual templates with Portrait/Landscape orientation instead of report/sub-reports templates.

2 – Build a collection of Byte[] to save all the generated reports as shown in the code below

private List<byte[]> generateReports(Map parameters, Object objet){
    	
    	List<byte[]> reports = new ArrayList<byte[]>();
    	JasperReport jasperReport = null;
    	JasperPrint jasperPrint = null;
    	HashMap labels = new HashMap();
    	
    	try {
    	
    	JRBeanCollectionDataSource jdc = jdsfactory.createBeanCollectionDatasource(object);
    	jasperReport = (JasperReport) JRLoader.loadObject("templateFile.jasper");
    	jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jdc);
    	reports.add(JasperExportManager.exportReportToPdf(jasperPrint));
	
	// Repeat the previous operations for all individual reports	
    		
    	} catch (Exception e) {
    		e.printStackTrace();
    	}

	return reports;
 }

3 – Call the PDF printer method to concatenate incoming files given as a collection of byte[] and build an output stream.

public static void sendConcatenatedPDFStream(List pdffiles, String fileName , HttpServletResponse response) throws Exception { 
		
		response.setContentType("application/pdf");
		response.setHeader("Pragma", "public");
		response.setHeader("Cache-Control", "max-age=0");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".pdf");
		
		int totallength = 0;
		List pdfs = new ArrayList();
		for (int i=0;i&lt;pdffiles.size();i++){
			pdfs.add(new ByteArrayInputStream(pdffiles.get(i)));
		}
		BufferedOutputStream out = new BufferedOutputStream( response.getOutputStream());
		Document document = new Document();
		try {
			List readers = new ArrayList();
			int totalPages = 0;
			Iterator iteratorPDFs = pdfs.iterator();
			
			while (iteratorPDFs.hasNext()) {
				InputStream pdf = iteratorPDFs.next();
				PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages();
			}
			
			// Create a writer for the outputstream
			PdfWriter writer = PdfWriter.getInstance(document, out);
			document.open();
			PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data PdfImportedPage page;
			int currentPageNumber = 0;
			int pageOfCurrentReaderPDF = 0;
			Iterator iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output.
			
			while (iteratorPDFReader.hasNext()) { 
				PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page.
				
				while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
					document.newPage(); pageOfCurrentReaderPDF++;
					currentPageNumber++;
					page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
					cb.addTemplate(page, 0, 0); totallength += pdfReader.getFileLength(); 
					}
				pageOfCurrentReaderPDF = 0; 
			}
			document.close();
			response.setContentLength(totallength);
			out.flush();
			out.close();
			
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
}
About these ads
  1. #1 by Lavanya on May 29, 2012 - 5:15 pm

    Thank you for your code, it is working fine

  1. JavaPins

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: