import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.pdfbox.exceptions.COSVisitorException;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentCatalog;
import org.pdfbox.pdmodel.PDPage;
public class ConcatPdf {
/**
* @param args
* @throws IOException
* @throws COSVisitorException
*/
public static void main(String[] args) throws IOException, COSVisitorException {
File f1 = new File(args[0]);
File f2 = new File(args[1]);
PDDocument doc1 = PDDocument.load(f1);
PDDocument doc2 = PDDocument.load(f2);
PDDocumentCatalog cot1 = doc1.getDocumentCatalog();
PDDocumentCatalog cot2 = doc2.getDocumentCatalog();
for(Object o : cot2.getAllPages()){
PDPage page = (PDPage)o;
doc1.addPage(page);
}
FileOutputStream fos = new FileOutputStream(new File(f1.getParent(), "out.pdf"));
doc1.save(fos);
fos.flush();
fos.close();
}
}
public void mergePages(ArrayList<File> files, OutputStream os) throws DocumentException, FileNotFoundException, IOException{
PdfCopyFields copy = new PdfCopyFields(os);
for(File f: files){
if(f.exists()){
PdfReader reader = new PdfReader(new FileInputStream(f));
copy.addDocument(reader);
reader.close();
}
}
}