This program helps you to learn how you can create a new pdf and to perform merging operation in pdf.
This program can be useful in situation where you want to operate on pdf. You can use this program to make a pdf programmatically and then fill it with your data.
Note : You need to download the following jar pdfbox-1.5.0.jar and commons-logging-1.1.1.jar
First we will import the classes:
Listing 1: Importing classes:
import java.io.File; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.util.PDFMergerUtility;
Here,
- File is for defining the path from where we will read the pdf’s to be combined into a single pdf
- PDDocument is for making a new PDF document
- PDPage is used to make a new page inside this pdf
- PDFMergerUtility is used to merge multiple pdf
Now, we will define the classes along with the main method,
Listing 2: Defining class with main method
public class Pdf { public static void main(String args[]) { new Pdf().createNew(); new Pdf().combine(); }
Here,
Now we define the combine method which will combine multiple pdf,
Listing 3: Combine Pdf
public void combine() { try { PDFMergerUtility mergePdf = new PDFMergerUtility(); String folder ="pdf"; File _folder = new File(folder); File[] filesInFolder; filesInFolder = _folder.listFiles(); for (File string : filesInFolder) { mergePdf.addSource(string); } mergePdf.setDestinationFileName("Combined.pdf"); mergePdf.mergeDocuments(); } catch(Exception e) { } }
Here,
- We will make an object of PDFMergerUtility which will be responsible for pdf combining.
- We make a variable to hold the folder where all the pdf to be combined exists
- We loop through each pdf in the folder and use the mergePdf.addSource to combine each pdf.
- setDestinationFileName is used to set the name of the final combined pdf
- mergeDocuments(); is used to merge the documents finally.
Listing 4: Create new PDF
public void createNew() { PDDocument document = null; try { String filename="test.pdf"; document=new PDDocument(); PDPage blankPage = new PDPage(); document.addPage( blankPage ); document.save( filename ); } catch(Exception e) { } } }
Here,
Full source code:
Listing 5: Full source code
package article14; import java.io.File; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.util.PDFMergerUtility; public class Pdf { public static void main(String args[]) { new Pdf().createNew(); new Pdf().combine(); } public void combine() { try { PDFMergerUtility mergePdf = new PDFMergerUtility(); String folder ="pdf"; File _folder = new File(folder); File[] filesInFolder; filesInFolder = _folder.listFiles(); for (File string : filesInFolder) { mergePdf.addSource(string); } mergePdf.setDestinationFileName("Combined.pdf"); mergePdf.mergeDocuments(); } catch(Exception e) { } } public void createNew() { PDDocument document = null; try { String filename="test.pdf"; document=new PDDocument(); PDPage blankPage = new PDPage(); document.addPage( blankPage ); document.save( filename ); } catch(Exception e) { } } }