Android create pdf

Android create pdf

When you develop an Android application that needs to create PDF file, this tip might be useful to you. In this tip, I am going to show you how to create a PDF file in Android using iTextG library. The iTextG library is the iText library ported to Android. It can be used to create and manipulate PDF document in Android. You can download the iTextG library from here.

To create a PDF file using iTextG, you follow the steps below.
1. Create a Document object.
2. Create a PdfWriter instance using its getInstance method. This method requires the Document object and FileOutputStream object. With the FileOutputStream object you can specify the output path of the PDF file.
3. Open the document by calling the open() method.
4. Write content to the PDF file using the add() method.
5. Close the document by calling the close() method.

Now to have a workable example application that used iTextG to create a PDF file, create a new Android project. Then modify the activity_main.xml file to add one EditText and one Button as shown below.

<LinearLayout xmlns:android=""

     android:hint="Type your text here"

     android:text="Create pdf" />


Extract the zip file that you downloaded and copy the itextg-5.5.2.jar file to the libs folder of your project in Android Studio. If you are using Eclipse, you have to add the itextg-5.5.2.jar file to your project build path (Project->Properties->Java Build Path->Add Jar...).

In the MainActivity class, you create a method called createPDF(). This method is called to create a new PDF file when the button is clicked.

package com.example.itextapp;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

public void createPDF(View view){
//reference to EditText
EditText et=(EditText)findViewById(;
//create document object
Document doc=new Document();
//output file path
String outpath=Environment.getExternalStorageDirectory()+"/mypdf.pdf";
try {
//create pdf writer instance
PdfWriter.getInstance(doc, new FileOutputStream(outpath));
//open the document for writing;
//add paragraph to the document
doc.add(new Paragraph(et.getText().toString()));
//close the document

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (DocumentException e) {
// TODO Auto-generated catch block


Before running the example application, you need to add permission to use external storage by adding the line below to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Posted by: Dara | post date: 08-07-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.