My web  project combines a few pdf files into a single pdf. I can store the 
combined pdf to a file no problem, It's when I try to send the combined pdf to 
the browser that nothing happens. I have tested sending a Hello World pdf to 
the browser and that works.  Here is my code. 



    if (pageAction.equals("combinePDF")) { 
     String thesePdfs = request.getParameter("thesePdfs"); 
      
     logger.info("Process these form id " + thesePdfs); 
     /* 
      don't want to create the combined pdf in memory there may not 
      be enough to handle it. 
     */ 
     ResourceBundle pdf_bundle = ResourceBundle.getBundle("pdffilepath"); 
     String outFile  = pdf_bundle.getString("combinedfiles") + "\\" + 
"CombinedPdf.pdf"; 

     StringTokenizer st = new StringTokenizer(thesePdfs, ","); 
     // used for testing not all formid's have a pdf 
//     StringTokenizer st = new StringTokenizer("575,751,761", ","); 
      
     response.setContentType("application/pdf"); 
     try { 
      int pageOffset = 0; 
      ArrayList masterBookMarkList = new ArrayList(); 

      Document document = null; 
      PdfCopy writer    = null; 
      PdfReader reader  = null; 
      boolean firstFile = true; 
       
      while(st.hasMoreElements()) {        
       /* 
        * with the form id 
        */ 
       int formId = Integer.parseInt(st.nextToken()); 
       logger.info("Getting DF for formid - " + formId); 
       DownloadForm df = downloadFormDao.select(formId); 
       /* 
        * if df is null the form doesn't exist a message about the non-existent 
        * form is to be returned 
        */ 
       if(df == null){ 
        // put error in session 
        logger.info("This form id " + formId + " is not in the DownloadForm 
table"); 
        continue; 
       } 
        
       /** 
        * Create a reader for the file that we are reading 
        */      
       reader = new PdfReader(df.getFilecontent()); 

       /** 
        * Replace all the local named links with the actual 
        * destinations. 
        */ 
       reader.consolidateNamedDestinations(); 

       /** 
        * Retrieve the total number of pages for this document 
        */ 
       int totalPages = reader.getNumberOfPages(); 
       logger.info("# of pages " + totalPages); 

       /** 
        * Get the list of bookmarks for the current document If the 
        * bookmarks are not empty, store the bookmarks into a master 
        * list 
        */ 
       logger.info("Checking for bookmarks..."); 
       List bookmarks = SimpleBookmark.getBookmark(reader); 
       if (bookmarks != null) { 
        if (pageOffset != 0) 
         SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); 
        masterBookMarkList.addAll(bookmarks); 
        logger.info("Bookmarks found and storing..."); 
       } else { 
        logger.info("No bookmarks in this file..."); 
       } 
       pageOffset += totalPages; 

       /** 
        * Merging the files to the first file. If we are passing file1, 
        * file2 and file3, we will merge file2 and file3 to file1. 
        */ 
       if(firstFile){ 
        /** 
         * Create the document object from the reader 
         */ 
        document = new Document(reader.getPageSizeWithRotation(1)); 
  
        /** 
         * Create a pdf writer that listens to this document. Any 
         * changes to this document will be written to the outFile 
         * 
         * outFile is a location where the final merged document 
         * will be written to. 
         */ 
  
        logger.info("Creating an empty PDF..."); 
        writer = new PdfCopy(document, new FileOutputStream(outFile)); 
//        writer = new PdfCopy(document, response.getOutputStream()); 
        /** 
         * Open this document 
         */ 
        document.open(); 
        firstFile = false; 
       } 
        
       /** 
        * Add the content of the file into this document (writer). Loop 
        * through multiple Pages 
        */ 
       PdfImportedPage page; 
       for (int currentPage = 1; currentPage <= totalPages; currentPage++) { 
        page = writer.getImportedPage(reader, currentPage); 
        writer.addPage(page); 
       } 

       /** 
        * This will get the documents acroform. This will return null 
        * if no acroform is part of the document. 
        * 
        * Acroforms are PDFs that have been turned into fillable forms. 
        */ 
       logger.info("Checking for Acroforms"); 
        
       PRAcroForm form = reader.getAcroForm(); 
       if (form != null) { 
        writer.copyAcroForm(reader); 
        logger.info("Acroforms found and copied"); 
       } else 
        logger.info("Acroforms not found for this file"); 
        
      } // end of selectledPdfFiles for loop 
       
      /** 
       * After looping through all the files, add the master bookmarklist. 
       * If individual PDF documents had separate bookmarks, master 
       * bookmark list will contain a combination of all those bookmarks 
       * in the merged document. 
       */ 
      if (!masterBookMarkList.isEmpty()) { 
       writer.setOutlines(masterBookMarkList); 
       logger.info("All bookmarks combined and added"); 
      } else { 
       logger.info("No bookmarks to add in the new file"); 
      } 

      writer.close(); 
      /** 
       * Finally Close the main document, which will trigger the pdfcopy 
       * to write back to the filesystem. 
       */ 
      document.close(); 

      logger.info("File has been merged and written to response output"); 
      logger.info("OutputStream string length " + 
response.getOutputStream().toString().length()); 
      logger.info(response.getOutputStream().toString()); 

      response.flushBuffer(); 
      logger.info("Have sent the combined PDF to the response output"); 
       
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
      
     return; 
    }     
------------------------------------------------------------------------------
LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99!
1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint
2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes
Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13. 
http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to