In this Android tip, I am going to show you how to use iTextG library to get and modify
metadata information of a PDF document. You need to add the iTextG library to the libs folder of your project before you can try the example application below. The metadata information includes title, subject, author, and keywords. Other metadata such as creator, producer, date created, and date modified were added automatically by the application used to generate the PDF document.
To get the metadata information of a PDF document, first you need to a PdfReader instance for the PDF file. Then use the
getInfo() method to get the metadata. The metadata is stored in a
HashMap object in the form of key/values pairs.
To modify the metadata, you need to create PdfStamper instance passing the PdfReader instance and a
FileOutputStream instance. The FileOutputStream instance creates a PDF file (if it does not exist) for the PDF document that is modified. After that you have to construct a HashMap object that contains key/value pairs of the metadata. Then add the HashMap object to the stamper using the
setMoreInfo() method. Finally close the stamper.
Now to have an example application to access metadata of a PDF document, you create a new Android project. Then add fours EditTexts, and one Button to the activity_main.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.inputdialog.MainActivity" >
<EditText
android:id="@+id/txttitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/txtsubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/txtauthor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/txtkeywords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:onClick="saveMetadata"
/>
</LinearLayout>
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity{
PdfStamper stamper;
EditText txtTitle;
EditText txtSubj;
EditText txtAuthor;
EditText txtKeywords;
PdfReader reader;
String filepath;
HashMap<String, String> metamap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTitle=(EditText)findViewById(R.id.txttitle);
txtSubj=(EditText)findViewById(R.id.txtsubject);
txtAuthor=(EditText)findViewById(R.id.txtauthor);
txtKeywords=(EditText)findViewById(R.id.txtkeywords);
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
filepath=intent.getData().getPath();
try {
reader = new PdfReader(filepath);
// Store pdf metadata in a HashMap
metamap = reader.getInfo();
// Display pdf metadata in EditTexts
txtTitle.setText("Title: "+metamap.get("Title"));
txtSubj.setText("Subject: "+metamap.get("Subject"));
txtAuthor.setText("Author: "+metamap.get("Author"));
txtKeywords.setText("Keywords: "+metamap.get("Keywords"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void saveMetadata(View view){
if(reader!=null){
try {
// Create PdfStamper instance
stamper = new PdfStamper(reader, new FileOutputStream(Environment.getExternalStorageDirectory()+"/myreport.pdf"));
// Create HashMap to store the metadata input by user
metamap = new HashMap<String,String>();
metamap.put("Title", txtTitle.getText().toString());
metamap.put("Subject", txtSubj.getText().toString());
metamap.put("Author", txtAuthor.getText().toString());
metamap.put("Keywords", txtKeywords.getText().toString());
// Add the metadata to the PdfStamper
stamper.setMoreInfo(metamap);
// Close stamper
stamper.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Before you run the example application, you need to declare WRITE_EXTERNAL_STORAGE permission to the AndroidManifest.xml file. To enable the application to receive the path of the PDF file selected from the file explorer, you have to declare an intent-filter in the AndroidManifest.xml file as shown below.
<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>
|
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.