Java+PDFBox.jarでPDFをページ分割する
JavaプログラムでPDFをページ毎に分割したいときがありますよね。
そのようなときに使うコードです。
public class PdfPageSplit {
/**
* PDFページをPDFのまま分割できるか?
*
* @param args
*/
public static void main(String[] args) {
// https://pdfbox.apache.org/download.cgi
// pdfbox-2.0.8
// fontbox-2.0.8.jar
String filepath = "file/pdf_ja_multipages.pdf";
File pdfFile = new File(filepath);
try {
PDDocument pdDoc = PDDocument.load(pdfFile);
int numberOfPages = pdDoc.getNumberOfPages();
System.err.println("pdDoc.getNumberOfPages():" + numberOfPages);
for (int n = 1; n <= numberOfPages; n++) {
System.err.println("n:" + n);
PDDocument doc = new PDDocument();
PDPage page = (PDPage) pdDoc.getPage(n - 1);
doc.addPage(page);
File tempFile = File.createTempFile("test", "_" + n + ".pdf");
System.err.println("tempFile:" + tempFile.getAbsolutePath());
doc.save(tempFile);
doc.close();
System.err.println("saved!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Author And Source
この問題について(Java+PDFBox.jarでPDFをページ分割する), 我々は、より多くの情報をここで見つけました https://qiita.com/oyahiroki/items/7075b7367642a30e6b93著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .