Android pdf document information

In this Android tip, I am going to show you how to get information of a PDF document. The information can be version of the application used to generate the PDF document, file size, number of pages, encryption, and size of a page.
You can achieve this goal using the iTextG library. You download the library from the http://sourceforge.net/projects/itextg/ link. Then extract the zip file and copy the itextg-5.5.2.jar file to the libs folder of your project.
Extracting the information of a PDF document is simple with iTextG. First, you need to create a PdfReader instance with the path of the PDF file to extract the information.  In the example application below, when a PDF file is selected to view from the file explorer, its path is captured and put in the PdfReader instance. The PdfReader have getPdfV
ersion(), getFileLength(), getNumberOfPages(), isEncrypted(), and getPageSize() methods to return the application version, size of the PDF file in bytes, number of pages in the PDF document, a boolean value indicating whether the PDF document is encrypted, and size of a page.

Now to have an example application on extracting information about a PDF document, you create a new Android project. Then add a TextView to the activity_main.xml file. It is used to display the information of the PDF document.
In the AndroidManifest.xml file, you have to declare an intent-filter to allow the application to receive the PDF file selected from the file explorer.


<intent-filter>
           <action android:name="android.intent.action.VIEW" />
           <action android:name="android.intent.action.PICK" />
           <category android:name="android.intent.category.DEFAULT" />
           <category android:name="android.intent.category.BROWSABLE" />
           <data android:scheme="file" />
           <data android:mimeType="application/pdf" />
           <data android:pathPattern=".*\\.pdf" />
           <data android:host="*" />
        </intent-filter>

Here is the code written in the MainActivity class to receive path of the selected file, and extract the information from the PDF document. The result information displays in the TextView.


import java.io.IOException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity{

   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     TextView tv=(TextView)findViewById(R.id.txtview);  
     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 path from the intent object
     String filepath=intent.getData().getPath();
     String pdfInformation="Pdf doc information: \r\n";
     try {
     PdfReader reader = new PdfReader(filepath);
     pdfInformation+="- Version: "+reader.getPdfVersion();
     pdfInformation+="\r\n- Length: "+reader.getFileLength() +" bytes";
     pdfInformation+="\r\n- Number of Pages: "+reader.getNumberOfPages();
     pdfInformation+="\r\n- Encrypted: "+reader.isEncrypted();
     Rectangle pageSize = reader.getPageSize(1);
     pdfInformation+="\r\n- Page size (Wdith, Height): "+pageSize.getWidth()+", "+pageSize.getHeight();
     tv.setText(pdfInformation);

     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }
     }
     }
   }
  
}


Android-pdf-document-information

Posted by: Dara | post date: 12-27-2014 | 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.