Hi guys,
At the end of this session you will have a clear idea about creating PDF's in android Application.Sometimes, you need to create certain applications where in you want to output the details in the form of a PDF file so that it could printed or used in some other way. Download the complete source code here.
At the end of this session you will have a clear idea about creating PDF's in android Application.Sometimes, you need to create certain applications where in you want to output the details in the form of a PDF file so that it could printed or used in some other way. Download the complete source code here.
Step 1:-
Create a New Android Project as "PDFCreation".
Step 2:-
In order to generate PDF we are going to use an external library called itext which is used for PDF creation.You can download the latest version of iText library from here.
Step 3:-
After downloading itext library, copy the jar file. Now go to Eclipse, open your project and paste the iText library file under the "libs" folder .
- Then Right click your project
- Click Build path,
- Configure Build Path.
A dialog box will appear as shown below:-
- Click Libraries Tab
- Add JARs
- Will show dialog which list all your projects as shown below:-
- Select your PDF creation project
- Go to the libs folder
- Select the iText jar library
- Then click Ok.
Step 4:-
Open your layout xml file from res/layout folder.
<RelativeLayout 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" >
<Button
android:id="@+id/createBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="145dp"
android:text="Create Pdf" />
</RelativeLayout>
Step 5:-
Open your java file from src folder.
In my project I have created as "CreatePdf.java".
package com.ruleandroid.pdfcreation;
import java.io.FileNotFoundException;
importproject
Click Build path,
Configure Build Path. java.io.FileOutputStream;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CreatePdf extends ActionBarActivity implements OnClickListener {
private static String FILE = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/filename.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD);
Button createPdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_pdf);
createPdf = (Button) findViewById(R.id.createBtn);
createPdf.setOnClickListener(this);
}
private void createPdf() {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addContent(document);
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
private static void addContent(Document document) throws DocumentException {
Anchor anchor = new Anchor("Anchor", catFont);
anchor.setName("Hello PDF");
// Second parameter is the number of the chapter
Chapter catPart = new Chapter(0);
Paragraph subPara = new Paragraph("Android PDF Created", subFont);
addEmptyLine(subPara, 1);
Section subCatPart = catPart.addSection(subPara);
Paragraph paragraph = new Paragraph();
addEmptyLine(paragraph, 5);
// subCatPart.add(paragraph);
// Add a table
createTable(subCatPart);
// Now add all this to the document
document.add(catPart);
}
private static void createTable(Section subCatPart)
throws BadElementException {
PdfPTable table = new PdfPTable(4);
PdfPCell c1 = new PdfPCell(new Phrase("Cell 1"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cell 2"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cell 3"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Cell 4"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
subCatPart.add(table);
}
// method to add empty line
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create_pdf, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.createBtn:
createPdf();
break;
default:
break;
}
}
}
Step 6:-
Add permission in AndroidManifest file as
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Step 7:-
Connect your real device to the development environment and run the application.
Step 8 :-
Once your application has launched click the create PDF button.After clicking, go to the sdcard directory your pdf will be created.