Join multiple PDF's with jasperReports

1

I hope someone can help me, I am trying to merge several PDFs into a single file but I only upload the files to an existing file. I would like to have it printed on a new website of my application. I have been using this line of code

JasperExportManager.exportReportToPdfStream (jp1, outStream); for a single file.

How could I do to join several pdf? I would greatly appreciate your comments ....

So I have my code so far

for (int i = 0; i < algo.length; i++) {


                results.put("EVALUATION_NUMBER", Integer.parseInt(algo[i]));

                LOG.info("All Generated PDF");
                jr = JasperCompileManager.compileReport(report);
                jasperPrint = JasperFillManager.fillReport(jr, results, con);

                jasperPrintList.add(jasperPrint);


                System.out.println(jasperPrintList.get(i));

                List<JRPrintPage> pages = jasperPrintList.get(i).getPages();

                  JasperPrint jp1 = new JasperPrint();

                    for (int j = 0; j < pages.size(); j++) {

                        JRPrintPage object = pages.get(j);
                        jp1.addPage(object);

                    }
    
asked by Dulce Core 02.06.2016 в 01:26
source

1 answer

1

As far as I know, Jasper can only generate PDFs from his templates, he does not have the functionality to join PDFs. To join PDFs, you can use a library that works with PDFs such as iText . Here is an example of how you can mix them with this library, adapted from this site :

public class ItextMerge {
    public static void main(String[] args) {
        List<InputStream> list = new ArrayList<InputStream>();
        try {
            // PDFs a unir
            list.add(new FileInputStream(new File("C:/Temp/1.pdf")));
            list.add(new FileInputStream(new File("C:/Temp/2.pdf")));

            // PDF final
            OutputStream out = new FileOutputStream(new File("C:/Temp/result.pdf"));

            doMerge(list, out);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    /**
     * Mezclar múltiples archivos PDF en uno
     * 
     * @param list Lista de InputStream de los archivos PDF a unir
     * @param outputStream Stream de archivo de salida del PDF
     * que resulta de la unión de los streams de la lista de entrada
     * @throws DocumentException
     * @throws IOException
     */
    public static void doMerge(List<InputStream> list, OutputStream outputStream)
            throws DocumentException, IOException {
        //crear un nuevo documento PDF
        Document document = new Document();
        //crear un escritor del PDF
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        //para cada PDF en la lista
        //leer su contenido por página e ir agregando
        //cada página en el PDF de la variable document
        for (InputStream in : list) {
            PdfReader reader = new PdfReader(in);
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                document.newPage();
                //import the page from source pdf
                PdfImportedPage page = writer.getImportedPage(reader, i);
                //add the page to the destination pdf
                cb.addTemplate(page, 0, 0);
            }
        }
        //cerrar streams para liberar recursos
        //y cualquier bloqueo de archivo
        outputStream.flush();
        document.close();
        outputStream.close();
    }
}
    
answered by 02.06.2016 в 17:19