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);
	// Repeat the previous operations for all individual reports	
    	} catch (Exception e) {

	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.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 =;
				PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages();
			// Create a writer for the outputstream
			PdfWriter writer = PdfWriter.getInstance(document, out);;
			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 =; // Create a new page in the target for each source page.
				while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
					document.newPage(); pageOfCurrentReaderPDF++;
					page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
					cb.addTemplate(page, 0, 0); totallength += pdfReader.getFileLength(); 
				pageOfCurrentReaderPDF = 0; 
		} catch (Exception e) { 

2 thoughts on “Build a jasper report merging pages with different format

Leave a Reply

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

You are commenting using your 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