Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Pdf operations in Java(Creating, merging pdf)

This program helps you to learn how you can create a new pdf and to perform merging operation in pdf.

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,

  1. File is for defining the path from where we will read the pdf’s to be combined into a single pdf
  2. PDDocument is for making a new PDF document
  3. PDPage is used to make a new page inside this pdf
  4. 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,

  • createNew method is used to make a new pdf
  • combine method is used to combine multiple pdf
  • 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,

    1. We will make an object of PDFMergerUtility which will be responsible for pdf combining.
    2. We make a variable to hold the folder where all the pdf to be combined exists
    3. We loop through each pdf in the folder and use the mergePdf.addSource to combine each pdf.
    4. setDestinationFileName is used to set the name of the final combined pdf
    5. 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,

  • An object of PDDocument is made to make a new document.
  • We use PDPage to make a new page inside the pdf doc
  • The new page is added through addPage method
  • Now we save the document using the save method.
  • 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)
    	{
    		
    	}
    }
    
    }
    


    My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

    What did you think of this post?
    Services
    [Close]
    To have full access to this post (or download the associated files) you must have MrBool Credits.

      See the prices for this post in Mr.Bool Credits System below:

    Individually – in this case the price for this post is US$ 0,00 (Buy it now)
    in this case you will buy only this video by paying the full price with no discount.

    Package of 10 credits - in this case the price for this post is US$ 0,00
    This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

    Package of 50 credits – in this case the price for this post is US$ 0,00
    This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


    > More info about MrBool Credits
    [Close]
    You must be logged to download.

    Click here to login