Android split pdf

In this Android tip, I am going to show you how to split a PDF file into multiple PDF files that each file has one page. Splitting a PDF file can be done simply using the iTextG library ( In Android Studio, to use the iTextG library, you need to download the library jar file from the link above. Then add it to the libs directory of your project. By default, the external library jar file is not detected. So to let Android Studio recognize it, from the project tree, you expands the libs directory and right-click the library jar file. Then select Add As Library. If you do not see the jar file, clean your project (Build-> Clean Project).

To split the PDF file using iTextG library, you need to read the file using PdfReader class. Then use the PdfCopy class to copy pages from an instance of the PdfReader and write them to new PDF files. You use the getImportedPage() and addPage() methods of the PdfCopy to import every page from the PdfReader and add it to the PdfCopy instance to write it out to a new file.
Now to have an example application on splitting a PDF file, you create a new Android project. In the AndroidManifest.xml file, you need to declare WRITE_EXTERNAL_STORAGE permission to allow the application to create new PDF files. Also, you define an intent-filter to allow the application to get the selected PDF file from the file explorer.

        android:label="@string/app_name" >
           <action android:name= "android.intent.action.MAIN" />
           <category android:name= "android.intent.category.LAUNCHER" />
           <action android:name= "android.intent.action.VIEW" />
           <action android:name= "android.intent.action.PICK" />
           <category android:name= "android.intent.category.DEFAULT" />
           <data android:scheme="file" />
           <data android:mimeType= "application/pdf" />
           <data android:pathPattern=".*\\.pdf" />
           <data android:host="*" />


In the activity_main.xml file, you need a TextView to display messages to the user.

<RelativeLayout xmlns:android= ""
   tools:context= "com.example.dara.myapp.MainActivity">



Here is the code written in the MainActivity class to get path of the selected file and split it into multiple PDF files.

public class MainActivity extends FragmentActivity {
   private TextView tv;
   private Handler handler;
   protected void onCreate(Bundle savedInstanceState) {
     handler=new Handler();
     // Get Intent object sent from file explorer
     Intent intent=getIntent();
        String action=intent.getAction();
        String type=intent.getType();
        if((Intent.ACTION_PICK.equals(action) || Intent.ACTION_VIEW.equals(action)) && type.endsWith("pdf")){
           // Get the file from the intent object
           final String filepath=intent.getData().getPath();
           // Split the pdf file
           tv.setText("Splitting "+filepath);
           handler.postDelayed(new Runnable(){
             public void run(){


           Toast.makeText(this,"Please select a pdf file to be split from file explorer ", Toast.LENGTH_SHORT).show();

   private void splitPdf(String src){
        Document doc=null;
        PdfReader pr=new PdfReader(src);
        PdfCopy pc=null;
        File f=new File(src);
        for(int i=1;i<=pr.getNumberOfPages();i++){ // looping through the pdf file
             doc=new Document();
             pc=new PdfCopy(doc, new FileOutputStream(f.getParent()+"/page"+i+".pdf"));

        tv.setText("The pdf is split successfully.");
     }catch(Exception e ) {

        tv.setText("The pdf is failed to split.");


Posted by: Dara | post date: 02-28-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.