将pdf和tiff添加到同一pdf文档并在浏览器中显示

时间:2013-10-25 13:37:17

标签: java jsp ftp itext apache-commons

我使用以下代码从ftp服务器检索文件并在浏览器中显示。

boolean fileFormatType = fileName.endsWith(".PDF");
     if (fileFormatType) {
        if (FilePdf != null && FilePdf.length() > 0) {
             is = ftp.retrieveFileStream(strFile);
             bis = new BufferedInputStream(is);
             response.reset();
             response.setContentType("application/pdf");
             response.setHeader("Content-Disposition",
                  "inline;filename=example.pdf");
             ServletOutputStream outputStream = response
                      .getOutputStream();
             System.out.println("here ");
             byte[] buffer = new byte[1024];
             int readCount;

             while ((readCount = bis.read(buffer)) > 0){
                 outputStream.write(buffer, 0, readCount);
             }
             outputStream.flush();
             outputStream.close();
         }
       } else {
             is = ftp.retrieveFileStream(strFile);
             ByteArrayOutputStream baos = convertTIFFtoPDF(is);
             response.setContentType("application/pdf");
             response.setContentLength(baos.size());
             response.setHeader("Content-disposition",
                    "attachment;filename=\"" + "importDocs.pdf" + "\"");
             ServletOutputStream out = response.getOutputStream();
             baos.writeTo(out);
             out.flush();
             out.close();

但现在,我需要在文件夹中添加多个文件(该文件夹可以包含PDF和TIFF),并在浏览器中一次显示所有文件。过去三天我一直在尝试失败。我可以发布我在这里尝试的代码,但我想要一个新的意见/方法。请帮我解决这个问题。我使用的是itextpdf5.1 commons-io.util apis

我得到一个负数组大小异常btw。

1 个答案:

答案 0 :(得分:1)

这是我问题的最终答案。

for (PdfBean pdfBean : list)
            {
                if(!pdfBean.getFileName().isEmpty()&&!pdfBean.getFileLocation().isEmpty()){


                msgDH.InsertUserHistDtls(pdfBean.getHawb(), userId, pdfBean.getCustomer(), pdfBean.getFileName(), pdfBean.getProduct(), pdfBean.getDocType(), "Print");
                msgDH.InsertLogDtls(pdfBean.getFileName(), pdfBean.getProduct(), pdfBean.getCustomer(), userId);

                FTPClient ftp = new FTPClient();
                int tiffPages = 0;
                try
                {
                    ftp.connect(ftpserver);
                } catch (SocketException e1)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                try
                {
                    boolean login = ftp.login(ftpusername, ftppassword);
                } catch (IOException e1)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                int reply;
                reply = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply))
                {
                    ftp.disconnect();
                    System.out.println("FTP server refused connection.");
                } else
                {
                    // ftp.enterLocalPassiveMode();
                    ftp.enterLocalActiveMode();
                    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                    ftp.changeWorkingDirectory(pdfBean.getFileLocation());
                    /* FTPFile[] */
                    ftpFiles = ftp.listFiles();
                    /* FTPFile[] */// ftpFiles = ftp.listFiles();
                    // ftp.completePendingCommand();
                }

                String fileName = pdfBean.getFileLocation() + "/"
                        + pdfBean.getFileName();
                if ("/interface/oracle/dds/generic/hold/TIF"
                        .equalsIgnoreCase(pdfBean.getFileLocation()))
                {
                    int i = 0;
                    String F = pdfBean.getFileName().replaceAll(".PDF",
                            ".TIF");
                    // FTPFile[] ftpFiles =
                    // ftp.listFiles(pdfBean.getFileLocation());
                    for (FTPFile ftpFile : ftpFiles)
                    {
                        if (ftpFile.getName().equals(F))
                        {
                            fileName = pdfBean.getFileLocation()
                                    .concat("/").concat(F);
                            i = 1;
                        }

                    }
                    if (i == 0)
                    {
                        F = pdfBean.getFileName().replaceAll(".PDF",
                                ".TIFF");
                        for (FTPFile ftpFile : ftpFiles)
                        {
                            if (ftpFile.getName().equals(F))
                            {
                                fileName = pdfBean.getFileLocation()
                                        .concat("/").concat(F);
                                i = 1;
                            }

                        }
                    }
                    int Repcode = ftp.getReplyCode();
                    //
                    // System.out.println(Repcode);
                    /* if(fileName.endsWith(".TIF")) */

                    InputStream is = (InputStream) ftp.retrieveFileStream(fileName);
                    Repcode = ftp.getReplyCode();
                    System.out.println(Repcode);
                    // ftp.completePendingCommand();
                    Repcode = ftp.getReplyCode();
                    System.out.println(Repcode);
                    ra1 = new RandomAccessFileOrArray(is);
                    tiffPages = TiffImage.getNumberOfPages(ra1);
                    System.out.println("No of pages in image is : "
                            + tiffPages);
                    for (int a = 1; a <= tiffPages; a++)
                    {
                        try
                        {
                            Image img = TiffImage.getTiffImage(ra1, a);
                            if (img != null)
                            {
                                if (img.getScaledWidth() > 500
                                        || img.getScaledHeight() > 700)
                                {
                                    img.scaleToFit(800, 800);
                                }
                                doc.setPageSize(new Rectangle(img
                                        .getScaledWidth(), img
                                        .getScaledHeight()));
                                img.setAbsolutePosition(0, 0);
                                cb.addImage(img);
                                // doc.
                                doc.newPage();
                                // ++pages;
                            }
                        } catch (Throwable e)
                        {
                            System.out.println("Exception " + " page "
                                    + (a + 1) + " " + e.getMessage());
                        }
                    }
                    is.close();
                    ra1.close();

                }
                else{

                    InputStream pdf = ftp.retrieveFileStream(fileName);
                    if(pdf != null) {
                        PdfReader pdfRea = new PdfReader(pdf);
                        readers.add(pdfRea);
                        totalPages += pdfRea.getNumberOfPages();   
                    }


                    /*totalPages += pdfRea.getNumberOfPages();


                    PdfImportedPage page;
                    int currentPageNumber = 0;
                    int pageOfCurrentReaderPDF = 0;
                    Iterator<PdfReader> iteratorPDFReader = readers.iterator();

                    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                            BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                   // 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()) {
                           doc.newPage();
                           pageOfCurrentReaderPDF++;
                           currentPageNumber++;
                           page = write.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                           cb.addTemplate(page, 0, 0);

                           // Code for pagination.
                           cb.beginText();
                           cb.setFontAndSize(bf, 9);
                           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                                   + currentPageNumber + " of " + totalPages, 520, 5, 0);
                           cb.endText();
                       }
                       pageOfCurrentReaderPDF = 0;
                   }*/
                }

                ftp.logout();
                ftp.disconnect();
                ftp = null;
            }}



            PdfImportedPage page;
            int currentPageNumber = 0;
            int pageOfCurrentReaderPDF = 0;
            Iterator<PdfReader> iteratorPDFReader = readers.iterator();

            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                    BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
           // 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()) {
                   doc.newPage();
                   pageOfCurrentReaderPDF++;
                   currentPageNumber++;
                   page = write.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                   cb.addTemplate(page, 0, 0);

                   // Code for pagination.
                   cb.beginText();
                   cb.setFontAndSize(bf, 9);
                   cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                           + currentPageNumber + " of " + totalPages, 520, 5, 0);
                   cb.endText();
               }
               pageOfCurrentReaderPDF = 0;
           }
        }
        doc.close();
        write.flush();
        write.close();
        System.out.println("done printing");

    }


    FileInputStream fiss = new FileInputStream(temp);  
    bis = new BufferedInputStream(fiss);
    response.reset();