DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat

Merging PDF’s with PDFBox

Merging Portable Document Format documents using PDFBox couldn’t be simpler.

Schalk Neethling user avatar by
Schalk Neethling
·
Jun. 21, 08 · Tutorial
Like (0)
Save
Tweet
Share
41.61K Views

Join the DZone community and get the full member experience.

Join For Free

Merging Portable Document Format documents using PDFBox  couldn’t be simpler. The developer(s) of PDFBox has taken care of all of the hard work and encapsulated it in one class of their Application Programming Interface. All you need to do is use it.

The class I am referring to is the PDFMergerUtility class. This class provides everything you need to take multiple single or multi page PDF documents and merge them into one PDF document. Below I will go over the simple steps of using this class to merge all PDF’s located in a directory without having to pass each file as an argument.

The first step is to initialize the class as follows:

PDFMergerUtility mergePdf = new PDFMergerUtility();

With the class initialized we can start to use it to merge our PDF’s. The next step in our process is to read and store the two arguments that gets passed into our application for later use. When invoking our utility from the command line we expect two arguments to be passed in, the first, the folder that contains the documents and the second, the file name of the final merged PDF. We store these arguments as two String variables:

String folder = args[0];String destinationFileName = args[1];

The next step is to get hold of all of the files in the directory that was passed to our utility and store them as a String variable called folder. For this I wrote a small method that uses the java.io.File class.

private static String[] getFiles(String folder) throws IOException{File _folder = new File(folder);String[] filesInFolder;   if(_folder.isDirectory()){filesInFolder = _folder.list();return filesInFolder;}else{throw new IOException("Path is not a directory");}}

The first thing we check is that the directory passed to us is in fact a directory. If not, we throw an IOException with the message Path is not a directory. After we verified that this is a directory we use the list() function from the java.io.File class to get the files from the directory. The list() method returns an array of all of the files in the directory. We store this in a String array and return this array to the caller.

String[] filesInFolder;   if(_folder.isDirectory()){filesInFolder = _folder.list();return filesInFolder;}

Because the final steps of our utility can possibly cause one of two exception two be thrown, we will enclose it within a try/catch block. The first thing we do inside our try block is to store the size of the array as an int variable called numberOfFiles, we will be using this inside our for loop a little later. Next we store our files in a String[] called, you guessed it, files. Armed with this information we can go ahead and loop through our array of files. The reason why we need to loop through our files is because we need to add them to the source of the PDFMergeUtility using it’s addSource function.

The for loop is then also where we will be making use of the first of our two variables, numberOfFiles.

for(int i = 0; i < numberOfFiles; i++)

Inside the loop we add each file to the PDFMergeUtility’s source using the following line of code:

mergePdf.addSource(folder + File.separator + files[i]);

The only steps left for us is to set the file name and location of the merged document and then call the PDFMergeUtility’s mergeDocuments() method.

mergePdf.setDestinationFileName(folder + File.separator + destinationFileName);mergePdf.mergeDocuments();

To close of our try block we catch the two possible exception that could be thrown by the methods used inside the try block. These are the COSVisitorException and an IOException. With this done our utility is complete! I hope you enjoyed this tutorial and find the utility useful. You can download the complete source here and use it as you see fit. Please feel free to post your comments as to how this utility can be improved and expanded upon.

PDF

Published at DZone with permission of Schalk Neethling. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Building Angular Library and Publishing in npmjs Registry
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Internal Components of Apache ZooKeeper and Their Importance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: