我们想使用Apache PDFBOXMerge功能-知道如何合并PDF文件-顺序。我有100页的PDF文件,该文件属于不同的客户,并且为每个客户编号,第5页的第1页,第5页的第2页,等等。
有没有一种方法可以建议合并顺序,还是必须对顺序进行硬编码,例如C:\ f \ test1.pdf,C:\ f \ test2.pdf,C:\ f \ test3。 pdf
我们想使用Apache PDFBOXMerge功能-知道如何合并PDF文件-顺序。我有100页的PDF文件,该文件属于不同的客户,并且为每个客户编号,第5页的第1页,第5页的第2页,等等。
有没有一种方法可以建议合并顺序,还是必须对顺序进行硬编码,例如C:\ f \ test1.pdf,C:\ f \ test2.pdf,C:\ f \ test3。 pdf
合并功能与文件名无关。如果要使用文件名合并pdf,则可以使用导入功能。
public static void main(String[] args) throws IOException {
PDDocument mergeDocument = new PDDocument();
PDDocument doc = null;
File dir = new File("folderPath"); //Provide directory path where all files are stored
File[] files = dir.listFiles();
for(File file: files) {
doc = PDDocument.load(file); //Load Pdf file
//Import Page one by one
for (int i = 0; i < doc.getNumberOfPages(); i++) {
mergeDocument.importPage(doc.getPage(i));
}
}
// save merged pdf
mergeDocument.save("location");
mergeDocument.close();
doc.close();
}
您可以根据需要获取文件列表,然后一页一页地导入
。