先下载pdfbox-app-1.7.1.jar包。下载地址:http://download.csdn.net/detail/yanning1314/4852276
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.util.PDFMergerUtility;
public class test {
/**
* @param args
*/
private static String[] getFiles(String folder) throws IOException
{
File _folder = new File(folder);
String[] filesInFolder;
if(_folder.isDirectory())
{
filesInFolder = _folder.list();
return filesInFolder;
}
else
{
throw new IOException("Path is not a directory");
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
PDFMergerUtility mergePdf = new PDFMergerUtility();
String folder = "F:/test";
String destinationFileName = "mergedTest.pdf";
String[] filesInFolder = getFiles(folder);
for(int i = 0; i < filesInFolder.length; i++)
mergePdf.addSource(folder + File.separator + filesInFolder[i]);
mergePdf.setDestinationFileName(folder + File.separator + destinationFileName);
mergePdf.mergeDocuments();
System.out.print("done");
}
}