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 (http://sourceforge.net/projects/itextg/.). 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.


<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
           <action android:name= "android.intent.action.MAIN" />
           <category android:name= "android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
           <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="*" />
        </intent-filter>

     </activity>

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

<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
   xmlns:tools=" http://schemas.android.com/tools"
   android:id="@+id/rootlayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context= "com.example.dara.myapp.MainActivity">

   <TextView
     android:id="@+id/txtmessage"
     android:text="@string/hello_world"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />

</RelativeLayout>


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;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     handler=new Handler();
     tv=(TextView)findViewById(R.id.txtmessage);
     // Get Intent object sent from file explorer
     Intent intent=getIntent();
     if(intent!=null){
        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(){
                 splitPdf(filepath);
             }

           },1000);


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

   private void splitPdf(String src){
     try{
        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"));
             doc.open();
             pc.addPage(pc.getImportedPage(pr,i));
             doc.close();

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

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

Android-split-pdf

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.