Results 1 to 2 of 2
  1. #1
    xampos20 is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default Merge 2 pdf files with itext and preview on servlet

    well hello again i would like if you can tell me .... what i am doing and its wrong and i cannot take results WHEN I EXECUTE MY SERVLET.....
    i want to
    "...MERGE TWO PDF INTO A NEW AND TO PREVIEW THE NEW ONE...."


    SOME OF MY CODE OF MY SERVLET:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    try {
    response.setContentType("application/pdf");
    List<InputStream> pdfs = new ArrayList<InputStream>();
    pdfs.add(new FileInputStream("c:\\ITextTest.pdf"));
    pdfs.add(new FileInputStream("c:\\Report1.pdf"));
    pdfs.add(new FileInputStream("c:\\merge.pdf"));
    OutputStream output = new FileOutputStream("c:\\result1.pdf");
    concatPDFs(pdfs, output, true);

    FileInputStream s= new FileInputStream("C:\\result1.pdf");
    output=response.getOutputStream();

    byte b[]=new byte[(int) s.getChannel().size()];
    s.read(b);
    output.write(b);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) {

    Document document = new Document();
    try {
    List<InputStream> pdfs = streamOfPDFFiles;
    List<PdfReader> readers = new ArrayList<PdfReader>();
    int totalPages = 0;
    Iterator<InputStream> iteratorPDFs = pdfs.iterator();

    // Create Readers for the pdfs.
    while (iteratorPDFs.hasNext()) {
    InputStream pdf = iteratorPDFs.next();
    PdfReader pdfReader = new PdfReader(pdf);
    readers.add(pdfReader);
    totalPages += pdfReader.getNumberOfPages();
    }
    // Create a writer for the outputstream
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);

    document.open();
    BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
    PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
    // data

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

    // 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()) {
    document.newPage();
    pageOfCurrentReaderPDF++;
    currentPageNumber++;
    page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
    cb.addTemplate(page, 0, 0);

    // Code for pagination.
    if (paginate) {
    cb.beginText();
    cb.setFontAndSize(bf, 9);
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0);
    cb.endText();
    }
    }
    pageOfCurrentReaderPDF = 0;
    }
    outputStream.flush();
    document.close();
    outputStream.close();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (document.isOpen())
    document.close();
    try {
    if (outputStream != null)
    outputStream.close();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    }


  2. #2
    xampos20 is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Merge Two Xml files ????
    By alwz_nikhil in forum XML
    Replies: 5
    Last Post: 01-18-2011, 09:18 AM
  2. Creating preview images of files in java
    By sandeepk84@gmail.com in forum Advanced Java
    Replies: 3
    Last Post: 07-04-2009, 10:29 PM
  3. Replies: 0
    Last Post: 06-29-2009, 02:40 PM
  4. How to Merge two Files
    By svpriyan in forum New To Java
    Replies: 2
    Last Post: 04-28-2009, 11:34 AM
  5. Replies: 1
    Last Post: 03-27-2008, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •