This discussion is archived
2 Replies Latest reply: Jan 28, 2013 1:06 PM by DrClap RSS

merge Pdf documents in Java

888525 Newbie
Currently Being Moderated
I have to merge two pdf.

Pdfs are already generated in a location on my server .I have to pick these pdfs from a specified location and merge into a single new pdf
file at the same location where pdf 1 and pdf 2 are there.I read a lot about it but could not find anything concrete,I tried itext but it doesnt works .It all the time gives me exception file niot found for the output file.

Can anyone help with methods to merge pdfs .
Please help

Thanks,
  • 1. Re: merge Pdf documents in Java
    DrClap Expert
    Currently Being Moderated
    You could certainly use PDFBox to merge two PDF documents -- you don't even need Java programming to do that. But if you're getting "file not found" exceptions, your problem isn't anything to do with PDF, it's just a basic beginner programming problem. You might want to clear that up (by posting a proper question about it) before you get on with the actual PDF work.
  • 2. Re: merge Pdf documents in Java
    888525 Newbie
    Currently Being Moderated
    Thankyou so much for the reply
    I saw an example online for pdfbox

    public class PDFMerge {

    public static void main(String[] args) throws Exception {
    // Get the byte streams from any source (maintain order)
    List<InputStream> sourcePDFs = new ArrayList<InputStream>();
    sourcePDFs.add(new FileInputStream(new File("pdf1.pdf")));
    sourcePDFs.add(new FileInputStream(new File("pdf2.pdf")));
    sourcePDFs.add(new FileInputStream(new File("pdf3.pdf")));

    // initialize the Merger utility and add pdfs to be merged
    PDFMergerUtility mergerUtility = new PDFMergerUtility();
    mergerUtility.addSources(sourcePDFs);
    // set the destination pdf name and merge input pdfs
    mergerUtility.setDestinationFileName("merged.pdf");
    mergerUtility.mergeDocuments();
    }
    }


    My confusion is under new File("pdf1.pdf")do i give the complete file path .I already have the generated pdf at a location in my server(http://localhost/test/temp/pdf1.pdf).This pdf is generated by some other application and placed at this location
    How do I specify the complete path for the input stream and similarly for the destination file also.
    similar kind of issue i was facing with itext but i see implementing PDFBox looks much easier.
    Please help

    Thanks,

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points