THIS CONTENT DOWNLOAD SHORTLY

Objective

The main objective of this post is to help you understand how to Generate PDF file in Android.

 

You will get Final Output:

name-properties

resume-name

 

Step 1 Download "itext" zip file

First we need following "Zip" file

  1. itext-5.3.2.zip

Download link: http://sourceforge.net/projects/itext/files/

 

Step 2 Get .jar files

Now Extract this zip file, and we need following "JAR" files.

  1. itextpdf-5.3.2.jar

  2. itextpdf-5.3.2-javadoc.jar

  3. itextpdf-5.3.2-sources.jar

 

Step 3 Import .jar files.

Add all three jar files into your Android Project.

  • Location: ProjectName >> libs
 

Step 4 onCreate() method

Now Start Coding for the Text to PDF.

  • First, open your Main Activity file. // Ex. MainActivity.java
  • Now add following code into your onCreate() method.
// Destination Folder and File name
String FILE = Environment.getExternalStorageDirectory().toString()
+"/PDF/"+"Name.pdf";
// Add Permission into Manifest.xml
//
 
// Create New Blank Document
Document document = new Document(PageSize.A4);
 
// Create Directory in External Storage
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/PDF");
myDir.mkdirs();	
 
// Create Pdf Writer for Writting into New Created Document
PdfWriter.getInstance(document, new FileOutputStream(FILE));
 
// Open Document for Writting into document
document.open();	
 
// User Define Method
addMetaData(document);	
addTitlePage(document);
// Close Document after writting all content
document.close();

For more information about Document please refer following link:

 

Step 5 addMetaData() method

Now outside of onCreate() method. Declare the User Define Method.

// Set PDF document Properties
public void addMetaData(Document document)
 
{
document.addTitle("RESUME");
document.addSubject("Person Info");
document.addKeywords("Personal,
Education, Skills");
document.addAuthor("TAG");
document.addCreator("TAG");
}
 

Step 6 addTitlePage() method

public void addTitlePage(Document document) throws DocumentException
  {
// Font Style for Document
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
| Font.UNDERLINE, BaseColor.GRAY);
Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
 
// Start New Paragraph
Paragraph prHead = new Paragraph();
// Set Font in this Paragraph
prHead.setFont(titleFont);
// Add item into Paragraph
prHead.add("RESUME – Name\n");
 
// Create Table into Document with 1 Row
PdfPTable myTable = new PdfPTable(1);
// 100.0f mean width of table is same as Document size
myTable.setWidthPercentage(100.0f);
 
// Create New Cell into Table
PdfPCell myCell = new PdfPCell(new Paragraph(""));
myCell.setBorder(Rectangle.BOTTOM);
 
// Add Cell into Table
myTable.addCell(myCell);
 
prHead.setFont(catFont);
prHead.add("\nName1 Name2\n");
prHead.setAlignment(Element.ALIGN_CENTER);
 
// Add all above details into Document
document.add(prHead);
document.add(myTable);
document.add(myTable);
 
// Now Start another New Paragraph
Paragraph prPersinalInfo = new Paragraph();
prPersinalInfo.setFont(smallBold);
prPersinalInfo.add("Address 1\n");
prPersinalInfo.add("Address 2\n");
prPersinalInfo.add("City: SanFran.  State: CA\n");
prPersinalInfo.add("Country: USA Zip Code: 000001\n");
prPersinalInfo.add("Mobile: 9999999999 Fax: 1111111 Email: john_pit@gmail.com \n");
 
prPersinalInfo.setAlignment(Element.ALIGN_CENTER);
 
document.add(prPersinalInfo);
document.add(myTable);
document.add(myTable);
 
Paragraph prProfile = new Paragraph();
prProfile.setFont(smallBold);
prProfile.add("\n \n Profile : \n ");
prProfile.setFont(normal);
prProfile.add("\nI am Mr. XYZ. I am Android Application Developer at TAG.");
 
prProfile.setFont(smallBold);
document.add(prProfile);
 
// Create new Page in PDF
document.newPage();
}
 

Step 7 Run Application

Run this App. Your PDF is now saved in the SD CARD with this path:

  • Location: sdcard >> PDF >> Name.pdf

name-pdf

For more details go on following links:

I hope you enjoy this tutorial and it would be helpful to you. Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

An entrepreneur who has founded 2 flourishing software firms in 7 years, Tejas is keen to understand everything about gaming - from the business dynamics to awesome designs to gamer psychology. As the founder-CEO of a company that has released some very successful games, he knows a thing or two about gaming. He shares his knowledge through blogs and talks that he gets invited to.