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="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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:orientation="vertical"
  
   >

   <EditText
     android:id="@+id/txt_input"
     android:layout_width="fill_parent"
     android:layout_height="100dp"
     android:gravity="top"
     android:hint="Type your text here"
    
     />

   <Button
     android:id="@+id/bt_createpdf"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
      android:onClick="createPDF"
     android:text="Create pdf" />
  


</LinearLayout>


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 java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

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


public void createPDF(View view){
//reference to EditText
EditText et=(EditText)findViewById(R.id.txt_input);
//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
doc.open();
//add paragraph to the document
doc.add(new Paragraph(et.getText().toString()));
//close the document
doc.close();

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

}

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" />

Android-create-pdf

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.