A PDF document can be divided into multiple PDF documents using a class called Splitter.
Below steps should be followed to split a PDF document.
To load an existing document, use the load() method of the PDDocument class. Instantiate this class to load the PDF document as depicted below. It accepts a file object as parameter.
Splitter class should be instantiated in order to use the methods in it for Splitting the document.
Splitter class has Split() method that is used to split the document which accepts an object of the PDDocument class as the parameter.
The function of the split() method is to split the each page of the document into individual documents and returns all these documents in form of a list.
After creating individual documents using split() method, we need to create an iterator object to traverse through the list of documents. We can get the iterator using the listIterator() method as depicted below.
Lastly, to close the document use close() method of the PDDocument class as depicted below.
Let us consider a PDF document by name sample.pdf in the path C:/PdfBox_Examples/ containing two pages – one having image and other having text as depicted below.
Example below explains on how to split above mentioned PDF document. We will load a PDF document named sample.pdf and split it into two documents sample1.pdf and sample2.pdf. Save the above code in a file by name SplitPages.java.
Once the file is saved, compile and execute it from command prompt using the below commands-
Above program will split the given PDF document. Below message is rendered upon execution-
Verify the path given while saving the document and you will see there are multiple PDFs were created with names sample1 and sample2 as depicted below.
PDFBox Related Interview Questions
|Adv Java Interview Questions||Core Java Interview Questions|
|Java applet Interview Questions||Java Developer Interview Questions|
|Java collections framework Interview Questions||Apache Hive Interview Questions|
|Apache Pig Interview Questions||Java Management Extensions (JMX) Interview Questions|
|Angular 5 Interview Questions||Apache Wicket Interview Questions|
|Java 9 Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.