Last updated on March 13th, 2020 at 11:52 am
In this post i am going to explain you how to create PDF in your android application. This post is very use full for those who want to generate PDF in mobile directly without any help of external app. For this post i am using iText library you can download it from here and after download add this in your project.
Note: Please use iTextG library reaming part is save
iText provides support for most advanced PDF features such as PKI-based signatures, 40-bit and 128-bit encryption, color correction, Tagged PDF, PDF forms (AcroForms), PDF/X, color management via ICC profiles and barcodes.
Step 1: Create a new project and add iText library to your project. To add library to your project go to: project ⇒ app ⇒ libs (paste your library .jar inside it and right click on your .jar add as library).
Step 2. Add read/write permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Step 3: Now open your activity_main.xml and paste bellow code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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=".MainActivity"> | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_below="@+id/textView1" | |
android:text="Generate PDF" /> | |
<ListView | |
android:id="@+id/list" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"></ListView> | |
</LinearLayout> |
Step 4: In your MainActivity.java we have to create a method to generate PDF file. I create a new method createPDF() inside my MainActivity.java file this is the main method which is responsible to generate PDF. Now to generate PDF we need to create layout so we create layout using iText table and cell
Sample code to work with iText Lib
PdfPTable pt = new PdfPTable(3); pt.setWidthPercentage(100); float[] fl = new float[]{20, 45, 35}; pt.setWidths(fl);
After table is create we can add cell in it like this
PdfPCell cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); cell.addElement(new Paragraph("Trinity Tuts")); cell.addElement(new Paragraph("")); cell.addElement(new Paragraph("")); pt.addCell(cell); // Add cell to table
If you want to add image in your cell you can do it in this way
Drawable myImage = MainActivity.this.getResources().getDrawable(R.drawable.trinity); Bitmap bitmap = ((BitmapDrawable) myImage).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bitmapdata = stream.toByteArray(); bgImage = Image.getInstance(bitmapdata); bgImage.setAbsolutePosition(330f, 642f); cell.addElement(bgImage); pt.addCell(cell);
Complete code to generate PDF
public void createPDF() throws FileNotFoundException, DocumentException { //create document file Document doc = new Document(); try { Log.e("PDFCreator", "PDF Path: " + path); SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy"); file = new File(dir, "Trinity PDF" + sdf.format(Calendar.getInstance().getTime()) + ".pdf"); FileOutputStream fOut = new FileOutputStream(file); PdfWriter writer = PdfWriter.getInstance(doc, fOut); //open the document doc.open(); //create table PdfPTable pt = new PdfPTable(3); pt.setWidthPercentage(100); float[] fl = new float[]{20, 45, 35}; pt.setWidths(fl); cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); //set drawable in cell Drawable myImage = MainActivity.this.getResources().getDrawable(R.drawable.trinity); Bitmap bitmap = ((BitmapDrawable) myImage).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bitmapdata = stream.toByteArray(); try { bgImage = Image.getInstance(bitmapdata); bgImage.setAbsolutePosition(330f, 642f); cell.addElement(bgImage); pt.addCell(cell); cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); cell.addElement(new Paragraph("Trinity Tuts")); cell.addElement(new Paragraph("")); cell.addElement(new Paragraph("")); pt.addCell(cell); cell = new PdfPCell(new Paragraph("")); cell.setBorder(Rectangle.NO_BORDER); pt.addCell(cell); PdfPTable pTable = new PdfPTable(1); pTable.setWidthPercentage(100); cell = new PdfPCell(); cell.setColspan(1); cell.addElement(pt); pTable.addCell(cell); PdfPTable table = new PdfPTable(6); float[] columnWidth = new float[]{6, 30, 30, 20, 20, 30}; table.setWidths(columnWidth); cell = new PdfPCell(); cell.setBackgroundColor(myColor); cell.setColspan(6); cell.addElement(pTable); table.addCell(cell); cell = new PdfPCell(new Phrase(" ")); cell.setColspan(6); table.addCell(cell); cell = new PdfPCell(); cell.setColspan(6); cell.setBackgroundColor(myColor1); cell = new PdfPCell(new Phrase("#")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 1")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 2")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 3")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 4")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 5")); cell.setBackgroundColor(myColor1); table.addCell(cell); //table.setHeaderRows(3); cell = new PdfPCell(); cell.setColspan(6); for (int i = 1; i <= 10; i++) { table.addCell(String.valueOf(i)); table.addCell("Header 1 row " + i); table.addCell("Header 2 row " + i); table.addCell("Header 3 row " + i); table.addCell("Header 4 row " + i); table.addCell("Header 5 row " + i); } PdfPTable ftable = new PdfPTable(6); ftable.setWidthPercentage(100); float[] columnWidthaa = new float[]{30, 10, 30, 10, 30, 10}; ftable.setWidths(columnWidthaa); cell = new PdfPCell(); cell.setColspan(6); cell.setBackgroundColor(myColor1); cell = new PdfPCell(new Phrase("Total Nunber")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Paragraph("Footer")); cell.setColspan(6); ftable.addCell(cell); cell = new PdfPCell(); cell.setColspan(6); cell.addElement(ftable); table.addCell(cell); doc.add(table); Toast.makeText(getApplicationContext(), "created PDF", Toast.LENGTH_LONG).show(); } catch (DocumentException de) { Log.e("PDFCreator", "DocumentException:" + de); } catch (IOException e) { Log.e("PDFCreator", "ioException:" + e); } finally { doc.close(); } } catch (Exception e) { e.printStackTrace(); } }
Complete MainActivity.java
package com.example; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.html.WebColors; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; public class MainActivity extends Activity { private Button b; private PdfPCell cell; private String textAnswer; private Image bgImage; ListView list; private String path; private File dir; private File file; //use to set background color BaseColor myColor = WebColors.getRGBColor("#9E9E9E"); BaseColor myColor1 = WebColors.getRGBColor("#757575"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button) findViewById(R.id.button1); list = (ListView) findViewById(R.id.list); //creating new file path path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Trinity/PDF Files"; dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { createPDF(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }); } @Override protected void onResume() { super.onResume(); //getting files from directory and display in listview try { ArrayList<String> FilesInFolder = GetFiles("/sdcard/Trinity/PDF Files"); if (FilesInFolder.size() != 0) list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FilesInFolder)); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // Clicking on items } }); } catch (NullPointerException e) { e.printStackTrace(); } } public ArrayList<String> GetFiles(String DirectoryPath) { ArrayList<String> MyFiles = new ArrayList<String>(); File f = new File(DirectoryPath); f.mkdirs(); File[] files = f.listFiles(); if (files.length == 0) return null; else { for (int i = 0; i < files.length; i++) MyFiles.add(files[i].getName()); } return MyFiles; } public void createPDF() throws FileNotFoundException, DocumentException { //create document file Document doc = new Document(); try { Log.e("PDFCreator", "PDF Path: " + path); SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy"); file = new File(dir, "Trinity PDF" + sdf.format(Calendar.getInstance().getTime()) + ".pdf"); FileOutputStream fOut = new FileOutputStream(file); PdfWriter writer = PdfWriter.getInstance(doc, fOut); //open the document doc.open(); //create table PdfPTable pt = new PdfPTable(3); pt.setWidthPercentage(100); float[] fl = new float[]{20, 45, 35}; pt.setWidths(fl); cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); //set drawable in cell Drawable myImage = MainActivity.this.getResources().getDrawable(R.drawable.trinity); Bitmap bitmap = ((BitmapDrawable) myImage).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bitmapdata = stream.toByteArray(); try { bgImage = Image.getInstance(bitmapdata); bgImage.setAbsolutePosition(330f, 642f); cell.addElement(bgImage); pt.addCell(cell); cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); cell.addElement(new Paragraph("Trinity Tuts")); cell.addElement(new Paragraph("")); cell.addElement(new Paragraph("")); pt.addCell(cell); cell = new PdfPCell(new Paragraph("")); cell.setBorder(Rectangle.NO_BORDER); pt.addCell(cell); PdfPTable pTable = new PdfPTable(1); pTable.setWidthPercentage(100); cell = new PdfPCell(); cell.setColspan(1); cell.addElement(pt); pTable.addCell(cell); PdfPTable table = new PdfPTable(6); float[] columnWidth = new float[]{6, 30, 30, 20, 20, 30}; table.setWidths(columnWidth); cell = new PdfPCell(); cell.setBackgroundColor(myColor); cell.setColspan(6); cell.addElement(pTable); table.addCell(cell); cell = new PdfPCell(new Phrase(" ")); cell.setColspan(6); table.addCell(cell); cell = new PdfPCell(); cell.setColspan(6); cell.setBackgroundColor(myColor1); cell = new PdfPCell(new Phrase("#")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 1")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 2")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 3")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 4")); cell.setBackgroundColor(myColor1); table.addCell(cell); cell = new PdfPCell(new Phrase("Header 5")); cell.setBackgroundColor(myColor1); table.addCell(cell); //table.setHeaderRows(3); cell = new PdfPCell(); cell.setColspan(6); for (int i = 1; i <= 10; i++) { table.addCell(String.valueOf(i)); table.addCell("Header 1 row " + i); table.addCell("Header 2 row " + i); table.addCell("Header 3 row " + i); table.addCell("Header 4 row " + i); table.addCell("Header 5 row " + i); } PdfPTable ftable = new PdfPTable(6); ftable.setWidthPercentage(100); float[] columnWidthaa = new float[]{30, 10, 30, 10, 30, 10}; ftable.setWidths(columnWidthaa); cell = new PdfPCell(); cell.setColspan(6); cell.setBackgroundColor(myColor1); cell = new PdfPCell(new Phrase("Total Nunber")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Phrase("")); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(myColor1); ftable.addCell(cell); cell = new PdfPCell(new Paragraph("Footer")); cell.setColspan(6); ftable.addCell(cell); cell = new PdfPCell(); cell.setColspan(6); cell.addElement(ftable); table.addCell(cell); doc.add(table); Toast.makeText(getApplicationContext(), "created PDF", Toast.LENGTH_LONG).show(); } catch (DocumentException de) { Log.e("PDFCreator", "DocumentException:" + de); } catch (IOException e) { Log.e("PDFCreator", "ioException:" + e); } finally { doc.close(); } } catch (Exception e) { e.printStackTrace(); } } }
Hope this will help u 🙂