- I create my db on a class call DBcreater .In that class i have my db, also the qurary that i use to get values for my PDF. So this method is give the data for my pdf from the db. i implement this on my DBcreater class
- public String getCustomerDataForDELPdf(String s1) {
// TODO Auto-generated method stub
SQLiteDatabase db = ourHelper.getReadableDatabase();
String selectQuery = "select cus_id,_id,customer_location,customer_phon,customer_shop,customer_email,customer_address,customer_name,customer_rep from Customer where customer_delear= '"+s1+"'" ;
Cursor c = ourDatabase.rawQuery(selectQuery, null);
String result="";
int iid=c.getColumnIndex(Key_customer_CId);
int iname=c.getColumnIndex(Key_customer_Name);
int iaddress=c.getColumnIndex(Key_customer_Address);
int icmp=c.getColumnIndex(Key_customer_Shop);
int icity=c.getColumnIndex(Key_customer_Location);
int iemail=c.getColumnIndex(Key_customer_Email);
int iphon=c.getColumnIndex(Key_customer_Phon);
int irep=c.getColumnIndex(Key_customer_Rep);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result=result+c.getString(iid)+"*"+c.getString(icmp)+"*"+c.getString(iname)+"*"+c.getString(icity)+"*"+c.getString(iaddress)+"*"+c.getString(iemail)+"*"+c.getString(iphon)+"*"+c.getString(irep)+"*";
}
db.close();
return result;
} - on my button click i call two class call createCustomerReport(s1); And
opencustomerReport(); - Button pdf = (Button)findViewById(R.id.bt_searchcustomer_pdf_Isuru);
pdf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
createCustomerReport(s1);
opencustomerReport();
}}); - On createCustomerReport(s1) method it will generate the pdf
- private void createCustomerReport(String s1) {
// TODO Auto-generated method stub
Document doc =new Document();
try {
//give the part for the doc
String parth = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";
//make object from file
File dir = new File(parth);
//make new pdf if not extit
if(!dir.exists())
dir.mkdir();
//
File file = new File(dir,"DelCusReport.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
doc.open();
String logger="Document Created by "+s1+" ";
Calendar cal = new GregorianCalendar();
String am_pm = (cal.get(Calendar.AM_PM)==0)?"AM":"PM";
String pdfheder=logger+" Date "+cal.get(Calendar.YEAR)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" Created on "+cal.get(Calendar.HOUR)+" : "+cal.get(Calendar.MINUTE)+" : "+cal.get(Calendar.SECOND)+" "+am_pm +"";
Paragraph p1= new Paragraph(pdfheder);
Font paraFont = new Font(Font.HELVETICA);
paraFont.setSize(36);
paraFont.setColor(Color.MAGENTA);
p1.setAlignment(Paragraph.ALIGN_LEFT);
p1.setFont(paraFont);
doc.add(p1);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.cusdatareport);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
//add image to document
doc.add(myImg);
Phrase footerText = new Phrase("Copy righs for jenasana groupes");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
// Make a table in pdf and name the columns
PdfPTable table = new PdfPTable(8);
PdfPCell c1 = new PdfPCell(new Phrase("Customer Id"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Customer Company"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Customer Name"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Customer City"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Customer Address"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Customer Email"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Customer Phon"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Customer Rep"));
c1.setBackgroundColor(Color.LIGHT_GRAY);
c1.setHorizontalAlignment(5);
table.addCell(c1);
table.setHeaderRows(1);
DBCreater db = new DBCreater(SearchCustomer.this);
db.openforread();
//db.getdata(); add data to the pdf table
String dataset=db.getCustomerDataForDELPdf(s1);
StringTokenizer token = new StringTokenizer(dataset,"*");
String s11,s2,s3,s4,s5,s6,s7,s8;
while(token.hasMoreTokens()){
s11 = token.nextToken();
table.addCell(s11);
s2 = token.nextToken();
table.addCell(s2);
s3 = token.nextToken();
table.addCell(s3);
s4 = token.nextToken();
table.addCell(s4);
s5 = token.nextToken();
table.addCell(s5);
s6 = token.nextToken();
table.addCell(s6);
s7 = token.nextToken();
table.addCell(s7);
s8 = token.nextToken();
table.addCell(s8);
}
//Anchor anc = new Anchor();
Paragraph preface = new Paragraph();
//Selection subcatpart =
preface.add(table);
doc.add(preface);
Toast.makeText(getApplicationContext(), "Created...", Toast.LENGTH_LONG).show();
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally
{
doc.close();
}
} - On opencustomerReport(); method open the pdf and display it
private void opencustomerReport() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_VIEW);
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";
/ //must give the early pdf file name in here as "DelCusReport.pdf"
File file = new File(path, "DelCusReport.pdf");
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent.setDataAndType( Uri.fromFile( file ), "application/pdf" ));
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent target = Intent.createChooser(intent, "Open File");
try {
startActivity(target);
} catch (ActivityNotFoundException e) {
Log.e("PDFCreator", "ActivityNotFoundException:" + e);
}
} - I use droidText.0.2.jar for make the pdf all so you must instal a pdf viewer to your emulator or phone like "KingsOffice"
Tuesday, June 3, 2014
Generate PDF Report in Android using SQL-LIGTH DB Values
Subscribe to:
Posts (Atom)