PDF Wirt DroidText
Here is my simple example which show how to genrate pdf file from android including text and images.
Here I keep this example simple, So you can understand easily.
But following are requirement for this project
Adding droidText to your project
There are two possibilities to add droidText to your project.
Android library project (recommended)
Check out droidText from svn and import it into your eclipse workspace (see here for checkout instructions and here for import instructions)
Add a reference to the droidText library project in your project as described in the Android Developer reference.
and here is our UI xml
Here is my simple example which show how to genrate pdf file from android including text and images.
Here I keep this example simple, So you can understand easily.
But following are requirement for this project
Adding droidText to your project
There are two possibilities to add droidText to your project.
Android library project (recommended)
Check out droidText from svn and import it into your eclipse workspace (see here for checkout instructions and here for import instructions)
Add a reference to the droidText library project in your project as described in the Android Developer reference.
package
com.itdeveloper.pdfformgenratorapp;
import
java.io.ByteArrayOutputStream;
import java.io.File;
import
java.io.FileOutputStream;
import java.io.IOException;
import
android.app.Activity;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import
android.os.Environment;
import android.util.Log;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
import
com.lowagie.text.Document;
import
com.lowagie.text.DocumentException;
import
com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import
com.lowagie.text.Image;
import
com.lowagie.text.Paragraph;
import
com.lowagie.text.Phrase;
import
com.lowagie.text.pdf.PdfWriter;
public class MainActivity extends Activity {
TextView
textView;
private String myString;
Document
doc;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button
genratePdf = (Button) findViewById(R.id.genrateBtn);
textView = (TextView)
findViewById(R.id.editText1);
genratePdf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated
method stub
myString = textView.getText().toString().trim();
Toast.makeText(getApplicationContext(),
myString, 1).show();
createPDF();
}
});
Button
openPdf = (Button) findViewById(R.id.Openpdfbtn);
openPdf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File
file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/droidText/mysample.pdf");
Intent
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
private void createPDF() {
doc = new Document();
try {
String
path = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/droidText";
File
dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
Toast.makeText(getApplicationContext(),
"Directory
Created", 1)
.show();
}
Log.d("PDFCreator", "PDF Path:
" +
path);
File
file = new File(dir, "mysample.pdf");
FileOutputStream
fOut = new
FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
// open the
document
ByteArrayOutputStream
stream = new ByteArrayOutputStream();
Bitmap
bitmap = BitmapFactory.decodeResource(getBaseContext()
.getResources(),
R.drawable.ic_launcher);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image
myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
// Write Actual
Text to Pdf in this funtion
addText();
// add image to
document
doc.add(myImg);
// set footer
Phrase
footerText = new Phrase("This is an example of a footer");
HeaderFooter
pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
}
catch (DocumentException
de) {
Log.e("PDFCreator", "DocumentException:" + de);
}
catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally {
doc.close();
}
}
/**
* Write Actual Text to Pdf in this funtion
*
* @throws DocumentException
* @throws IOException
*/
private void addText() throws DocumentException,
IOException {
// TODO Auto-generated
method stub
doc.open();
Paragraph
p1 = new Paragraph("FORM");
Font
paraFont = new Font(Font.COURIER, 45.00f, Font.BOLD);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
// add paragraph to
document
doc.add(p1);
Paragraph
p2 = new Paragraph("This is an
example of a simple paragraph");
Font
paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
p2.setAlignment(Paragraph.ALIGN_LEFT);
p2.setFont(paraFont2);
doc.add(p2);
Paragraph
p3 = new Paragraph(myString);
Font
paraFont3 = new Font(Font.COURIER, 14.0f, Color.GREEN);
p2.setAlignment(Paragraph.ALIGN_LEFT);
p2.setFont(paraFont3);
doc.add(p3);
}
}
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/genrateBtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="26dp"
android:text="Genrate Pdf" />
<Button
android:id="@+id/Openpdfbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/genrateBtn"
android:layout_alignRight="@+id/genrateBtn"
android:layout_below="@+id/genrateBtn"
android:text="Open Pdf" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="Type to add text in pdf" >
<requestFocus />
</EditText>
</RelativeLayout>
WE REQUIR TWO PERMITIONS AS
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
////////////////////////////////////////
and here is your manifest xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itdeveloper.pdfformgenratorapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itdeveloper.pdfformgenratorapp.MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
YOU CAN DOWNLOAD COMPLETE EXAMPLE FROM HERE
Happpy Coddding :)
No comments:
Post a Comment