将pdf和tiff添加到同一pdf文档并在浏览器中显示 [英] Add pdf and tiff to the same pdf document and display it in the browser

查看:157
本文介绍了将pdf和tiff添加到同一pdf文档并在浏览器中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am using the below code to retrieve a file from an ftp server and display it in the browser.

使用下面的代码从ftp服务器检索文件并将其显示在浏览器中。 = fileName.endsWith(。PDF); (文件格式类型){
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 =响应
.getOutputStream();
System.out.println(here);
byte [] buffer = new byte [1024];
int readCount; ((readCount = bis.read(buffer))> 0){
outputStream.write(buffer,0,readCount);

while(
}
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();

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

But now, i need to add the multiple files in a folder (the folder could contain PDFs and TIFFs) and display them all at a time in a browser. I have been trying unsuccessfully for the past three days. I could post the code that i tried here but i want a fresh opinion/approach. Please help me solve this. I am using itextpdf5.1 commons-io.util apis

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

I get a negative array size exception btw.

推荐答案

这是我的问题的最终答案。

This is the final answer to my problem.

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();

这篇关于将pdf和tiff添加到同一pdf文档并在浏览器中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆