Home > Mobile >  How can I merge two pdf using pdfBox
How can I merge two pdf using pdfBox

Time:03-03

Hi there I am trying to generate pdf by combining two one from my local machine and another one from s3 I am not sure how to do this here is what I tried doing -

S3Object object = amazonS3.getObject(new GetObjectRequest(bucket, "name.pdf"));
InputStream ins = object.getObjectContent();
try {
    PDDocument doc = PDDocument.load(ins);

    File file1 = new File(
            "C:\\Users\\admin\\example.pdf");
    PDFMergerUtility obj = new PDFMergerUtility();

    obj.setDestinationFileName(
            "C:\\Users\\admin\\newMerged.pdf");

    obj.addSource(file1);
    obj.addSource(String.valueOf(doc));
    obj.mergeDocuments();
    System.out.println("PDF Documents merged to a single file");
}
catch (Exception e){
    e.printStackTrace();
}

Error - org.apache.pdfbox.pdmodel.PDDocument@4d33940d (The system cannot find the file specified)

I know this could not be the way, I wanna know how to do this thing.

CodePudding user response:

Consider to use absolute pathname to where the file exists.

PDFMergerUtility result = new PDFMergerUtility();
result.addSource("C:\\...\\FileRead\\first.txt");
result.addSource("C:\\...\\FileRead\\second.txt");
result.setDestinationFileName("the destination path");
result.mergeDocuments();
  • Related