Paging PDF with Java + PDFBox.jar


public class PdfPageSplit {

	/**
	 *Can a PDF page be split as a 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();
		}
	}
}

Recommended Posts

Paging PDF with Java + PDFBox.jar
Output PDF and TIFF with Java 8
Read a string in a PDF file with Java
Text extraction in Java from PDF with pdfbox-2.0.8
Install java with Homebrew
[Java] Convert PDF version
Change seats with java
Install Java with Ansible
Java compressed PDF document
Switch java with direnv
[Java] PDF viewing settings
Download Java with Ansible
Let's scrape with Java! !!
Build Java with Wercker
Endian conversion with JAVA
Export pdf with a single program (Java / Perl / VBA)
I tried OCR processing a PDF file with Java
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Java Config with Spring MVC
Let's experiment with Java inlining
Java adds table to PDF
[Template] MySQL connection with Java
Rewrite Java try-catch with Optional
Install Java 7 with Homebrew (cask)
[Java] JSON communication with jackson
Java to play with Function
Save Java PDF in Excel
Try DB connection with Java
Enable Java EE with NetBeans 9
[Java] JavaConfig with Static InnerClass
Try gRPC with Java, Maven
Let's operate Excel with Java! !!
Version control Java with SDKMAN
RSA encryption / decryption with java 8
Sort strings functionally with java
Object-oriented (java) with Strike Gundam
Java turns Excel into PDF
Save Java HTML as PDF
[Java] Content acquisition with HttpCliient
Java version control with jenv
Troubleshooting with Java Flight Recorder
Streamline Java testing with Spock
Connect to DB with Java
Connect to MySQL 8 with Java
Error when playing with java
Using Mapper with Java (Spring)
Java study memo 2 with Progate
Getting Started with Java Basics
Seasonal display with Java switch
Use SpatiaLite with Java / JDBC
Study Java with Progate Note 1
Compare Java 8 Optional with Swift
I tried OCR processing a PDF file with Java part2
HTML parsing with JAVA (scraping)
Run Java VM with WebAssembly
Screen transition with swing, java
Java unit tests with Mockito
[Java 8] Duplicate deletion (& duplicate check) with Stream
Create an immutable class with JAVA