Created
January 15, 2021 17:35
-
-
Save aspose-com-gists/dbd370f2a932f1dc591bee6222550150 to your computer and use it in GitHub Desktop.
Split PDF Files in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open document | |
Document pdfDocument = new Document("input.pdf"); | |
// Create array lists for even and odd pages | |
List<Page> evenPages = new ArrayList<Page>(); | |
List<Page> oddPages = new ArrayList<Page>(); | |
// Loop through all the pages | |
for (Page pdfPage : pdfDocument.getPages()) | |
{ | |
if(pdfPage.getNumber()%2==0) | |
{ | |
evenPages.add(pdfPage); | |
} | |
else | |
{ | |
oddPages.add(pdfPage); | |
} | |
} | |
// Save even pages as PDF | |
Document newDocument = new Document(); | |
newDocument.getPages().add(evenPages); | |
newDocument.save("split_even_Pages.pdf"); | |
// Save odd pages as PDF | |
newDocument = new Document(); | |
newDocument.getPages().add(oddPages); | |
newDocument.save("split_odd_Pages.pdf"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open document | |
Document pdfDocument = new Document("input.pdf"); | |
// For page counter | |
int pageCount = 1; | |
// Loop through all the pages | |
for (Page pdfPage : pdfDocument.getPages()) { | |
// Create a new document | |
Document newDocument = new Document(); | |
// Add page to the document | |
newDocument.getPages().add(pdfPage); | |
// Save as PDF | |
newDocument.save("page_" + pageCount + "_out" + ".pdf"); | |
pageCount++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment