import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.Loader; import org.apache.pdfbox.text.PDFTextStripper; import java.io.File; import java.io.IOException; public class PDFSplitter { public static void main(String[] args) { try { String splitter = "%%SPLIT"; PDDocument document = Loader.loadPDF(new File(args[0])); int countPages = document.getNumberOfPages(); PDDocument newDocument = new PDDocument(); for (int page = 0; page < countPages; page++) { newDocument.addPage((PDPage) document.getPage(page)); PDFTextStripper pdfTextStripper = new PDFTextStripper(); String text = pdfTextStripper.getText(newDocument); if (text.contains(splitter)) { String fileName = text.substring(text.indexOf(splitter)); fileName = fileName.substring(splitter.length()+1, fileName.indexOf('\r'))+".pdf"; File f = new File(fileName); if (f.exists()) { f.delete(); } String outputFileName = fileName; newDocument.save(outputFileName); newDocument.close(); newDocument = new PDDocument(); } } document.close(); } catch (IOException e) { e.printStackTrace(); } } }