How to display reports in pdf format in android

14/04/2017   06:35:33 PM
Hello in this android article we will create android application which creates reports in pdf format or export in PDF.in this tutorial we will use iTEXT open source library to create reports in PDF format
 
 1. Download itextg-5.5.10.zip .jar file from https://github.com/itext/itextpdf/releases/tag/5.5.10

 

 2. Add the following code to GradleScripts/build.Graddle(Module:app) file

dependencies
{
        compile files ('libs/itextg-5.5.10.jar')
}

 

3. Click on syn now option

display reports in pdf format in android

 

4. Add the following code in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.chxii.xperiaitinnovations.pdfgen.MainActivity"
    tools:showIn="@layout/activity_main">
 <EditText
     android:id="@+id/txt_input"
     android:layout_width="fill_parent"
     android:layout_height="100dp" />
  <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Creat PDF"
        android:layout_below="@+id/txt_input"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
         />
</RelativeLayout>

 

 5. Add following code in MainActivity.java

import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {
Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View view) {
         Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
         .setAction("Action", null).show();
           }
 });

       // EditText txt = (EditText) findViewById(R.id.txt_input);
  
Button btn = (Button) findViewById(R.id.btn);
  btn.setOnClickListener(new View.OnClickListener() { @Override
  public void onClick(View v) {

  EditText txt = (EditText) findViewById(R.id.txt_input);
  Document document = new Document();
  String outPath = Environment.getExternalStorageDirectory() + "/mypdf.pdf";  

  try {
                PdfWriter.getInstance(document, new FileOutputStream(outPath));
                document.open();
                document.add(new Paragraph(txt.getText().toString()));
                document.close();
        }  

catch (DocumentException e){
                e.printStackTrace();
            }  

catch (FileNotFoundException e)   {
                e.printStackTrace();
            }
        }

});
 }

 

OUTPUT 

display reports in pdf format in android

 

display reports in pdf format in android


Output

                   

Support us by Sharing