Your source code is now downloading. If does not start download please wait for 10 seconds

Your source code is now downloading. If does not start download please wait for 10 seconds

Created By Tejas Jasani | Last Updated on : 16 January 2016

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

You will get Final Output:

Step 1 Download "itext" zip file

First we need following "Zip" file

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.

itextpdf-5.3.2.jar itextpdf-5.3.2-javadoc.jar 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

"); // 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("

Name1 Name2

"); 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

"); prPersinalInfo.add("Address 2

"); prPersinalInfo.add("City: SanFran. State: CA

"); prPersinalInfo.add("Country: USA Zip Code: 000001

"); prPersinalInfo.add("Mobile: 9999999999 Fax: 1111111 Email: [email protected]

"); prPersinalInfo.setAlignment(Element.ALIGN_CENTER); document.add(prPersinalInfo); document.add(myTable); document.add(myTable); Paragraph prProfile = new Paragraph(); prProfile.setFont(smallBold); prProfile.add("



Profile :

"); prProfile.setFont(normal); prProfile.add("

I 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

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.