I am developing an android billing application for Android devices.In my application i had to print the receipts in the Bluetooth printer in the specific format as the activity displays ,which i showned below.i have to print the whole screen as i showned below. i want to know how to recognize nearest Bluetooth printers?,How to configure them ?and also how to print data in a Bluetooth printer. the app is Compatible for previous version. I searched lot but not got any positive response.
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
I will tell you what i did for my app.may be that won't be suit for you.First of all you have to convert the activity into pdf using itext . then save the pdf into sdcard.from there you have to open the pdf using any application which supports printing.you have to install that application into your device.i used PrinterShare . for me its works fine.it fulfilled my needs. the code is given below.
Document doc = new Document();
PdfWriter docWriter = null;
DecimalFormat df = new DecimalFormat("0.00");
try {
Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12,
Font.BOLD, new BaseColor(0, 0, 0));
Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/noufalpdfdemo";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "sample.pdf");
docWriter = PdfWriter.getInstance(doc,
new FileOutputStream(file));
// document header attributes
doc.addAuthor("betterThanZero");
doc.addCreationDate();
doc.addProducer();
doc.addCreator("MySampleCode.com");
doc.addTitle("Report with Column Headings");
doc.setPageSize(PageSize.LETTER);
// open document
doc.open();
// create a paragraph
PdfContentByte cb = docWriter.getDirectContent();
Paragraph paragraph = new Paragraph("", FontFactory
.getFont(FontFactory.TIMES_BOLD, 24, BaseColor.RED));
paragraph.setAlignment(Element.ALIGN_CENTER);
Paragraph para = new Paragraph("INVOICE", FontFactory
.getFont(FontFactory.TIMES_BOLD, 24, BaseColor.RED));
para.setAlignment(Element.ALIGN_CENTER);
doc.add(para);
cr = db.getcustomerinvoices(getIntent().getStringExtra("i"));
String invoffitemdiscounttotal, invoffinvoicediscounttotal, invdiscount, invnettamount;
String date, time, cus_id, c_name, invoiceitem, granttotal;
cr.moveToFirst();
date = cr.getString(cr.getColumnIndex("date"));
time = cr.getString(cr.getColumnIndex("time"));
cus_id = cr.getString(cr.getColumnIndex("cus_id"));
c_name = cr.getString(cr.getColumnIndex("c_name"));
invoiceitem = cr.getString(cr.getColumnIndex("invoiceitem"));
granttotal = cr.getString(cr.getColumnIndex("granttotal"));
invoffitemdiscounttotal = cr.getString(cr
.getColumnIndex("invoffitemdiscounttotal"));
invoffinvoicediscounttotal = cr.getString(cr
.getColumnIndex("invoffinvoicediscounttotal"));
invdiscount = cr.getString(cr.getColumnIndex("Discount"));
invnettamount = cr.getString(cr.getColumnIndex("NetAmount"));
// add some detail information about the country
doc.add(new Paragraph("CUS NAME: " + c_name.trim(), bf12));
doc.add(new Paragraph("CUS ID: " + cus_id.trim(), bf12));
doc.add(new Paragraph("DATE: " + date.trim(), bf12));
doc.add(new Paragraph("INVOICE NUMBER: "
+ invoicenumber.trim(), bf12));
doc.add(Chunk.NEWLINE);
doc.add(Chunk.NEWLINE);
float[] columnWidths = { 2.5f, 1f, 2f, 2f, 2f, 2f };
// create PDF table with the given widths
PdfPTable table = new PdfPTable(columnWidths);
// set table width a percentage of the page width
table.setWidthPercentage(90f);
insertCell(table, "Item", Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, "QTY", Element.ALIGN_LEFT, 1, bfBold12);
insertCell(table, "PRICE", Element.ALIGN_LEFT, 1, bfBold12);
insertCell(table, "TOTAL", Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, "DISCOUNT", Element.ALIGN_LEFT, 1,
bfBold12);
insertCell(table, "TOTAL", Element.ALIGN_RIGHT, 1, bfBold12);
table.setHeaderRows(1);
// insert an empty row
// insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
cr = db.getcustomerinvoicetable(getIntent().getStringExtra(
"i"));
if (cr.getCount() > 0) {
cr.moveToFirst();
for (int i = 0; i < cr.getCount(); i++) {
String name = cr.getString(cr
.getColumnIndex("itemname"));
String price = cr.getString(cr
.getColumnIndex("price"));
String qty = cr.getString(cr.getColumnIndex("qty"));
String total = cr.getString(cr
.getColumnIndex("total"));
String discount = cr.getString(cr
.getColumnIndex("DiscountAmt"));
String newtotal = cr.getString(cr
.getColumnIndex("NetAmount"));
insertCell(table, name, Element.ALIGN_RIGHT, 1,
bf12);
insertCell(table, qty, Element.ALIGN_LEFT, 1, bf12);
insertCell(table, price, Element.ALIGN_LEFT, 1,
bf12);
insertCell(table, total, Element.ALIGN_RIGHT, 1,
bf12);
insertCell(table, discount, Element.ALIGN_LEFT, 1,
bf12);
insertCell(table, newtotal, Element.ALIGN_RIGHT, 1,
bf12);
cr.moveToNext();
}
// insertCell(table, "", Element.ALIGN_RIGHT, 1,
// bfBold12);
// insertCell(table, "", Element.ALIGN_RIGHT, 1,
// bfBold12);
//
// insertCell(table, "", Element.ALIGN_RIGHT, 1,
// bfBold12);
// insertCell(table, "", Element.ALIGN_RIGHT, 1,
// bfBold12);
// insertCell(table, "", Element.ALIGN_RIGHT, 1,
// bfBold12);
// insertCell(table, "", Element.ALIGN_RIGHT, 1,
// bfBold12);
insertCell(table, "", Element.ALIGN_RIGHT, 6, bfBold12);
insertCell(table, "GrandTotal:", Element.ALIGN_RIGHT,
2, bfBold12);
insertCell(table, granttotal, Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "OFF LINE ITEM PROMO TOTAL DISCOUNT:", Element.ALIGN_RIGHT,
2, bfBold12);
insertCell(table, invoffitemdiscounttotal, Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "OFF INVOICE PROMO TOTAL DISCOUNT:", Element.ALIGN_RIGHT,
2, bfBold12);
insertCell(table, invoffinvoicediscounttotal, Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "TOTAL DISCOUNT:", Element.ALIGN_RIGHT,
2, bfBold12);
insertCell(table, invdiscount, Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "NET AMOUNT:", Element.ALIGN_RIGHT,
2, bfBold12);
insertCell(table, invnettamount, Element.ALIGN_LEFT, 4,
bfBold12);
}
// repeat the same as above to display another location
insertCell(table, "", Element.ALIGN_LEFT, 6, bfBold12);
insertCell(table, "FREE GOODS REVIEW ...",
Element.ALIGN_CENTER, 6, bfBold12);
insertCell(table, "Item", Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, "QTY", Element.ALIGN_LEFT, 1, bfBold12);
insertCell(table, "", Element.ALIGN_CENTER, 6, bfBold12);
table.setHeaderRows(1);
// insert an empty row
// insertCell(table, "GrandTotal:", Element.ALIGN_RIGHT, 3,
// bfBold12);
// insertCell(table, granttotal, Element.ALIGN_RIGHT, 1,
// bfBold12);
Cursor cr2 = db.getinvoiceextras(invoicenumber);
if (cr2.getCount() > 0) {
cr2.moveToFirst();
String fockey = cr2.getString(cr2
.getColumnIndex("fockey"));
String accrevkey = cr2.getString(cr2
.getColumnIndex("accrevkey"));
Cursor cr3 = db.getcusinvfocdatas(fockey);
if (cr3.getCount() > 0) {
cr3.moveToFirst();
for (int i = 0; i < cr3.getCount(); i++) {
String name = cr3.getString(cr3
.getColumnIndex("focitemname"));
String qty = cr3.getString(cr3
.getColumnIndex("focqty"));
insertCell(table, name, Element.ALIGN_RIGHT, 1,
bf12);
insertCell(table, qty, Element.ALIGN_LEFT, 1,
bf12);
cr3.moveToNext();
}
}
else {
Log.d("nzm",
"else for cr3:db.getcusinvfocdatas(fockey) for "
+ fockey);
}
insertCell(table, "", Element.ALIGN_LEFT, 6, bfBold12);
// insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
insertCell(table, "ACCOUNTS REVIEW ...",
Element.ALIGN_CENTER, 6, bfBold12);
Cursor cr4 = db.getcusinvaccrevdatas(accrevkey);
if (cr4.getCount() > 0) {
cr4.moveToFirst();
oldbalance = cr4.getString(cr4
.getColumnIndex("oldbalance"));
curosbalance = cr4.getString(cr4
.getColumnIndex("curosbalance"));
amtcollected = cr4.getString(cr4
.getColumnIndex("amtcollected"));
paymode = cr4.getString(cr4
.getColumnIndex("paymode"));
cheque = cr4.getString(cr4
.getColumnIndex("ischeque"));
bank = cr4.getString(cr4.getColumnIndex("bank"));
chequenumber = cr4.getString(cr4
.getColumnIndex("chequenumber"));
chequeamount = cr4.getString(cr4
.getColumnIndex("chequeamount"));
chequedate = cr4.getString(cr4
.getColumnIndex("chequedate"));
pdccheque = cr4.getString(cr4
.getColumnIndex("ispdc"));
bolcheque = Boolean.parseBoolean(cheque);
bolpdc = Boolean.parseBoolean(pdccheque);
insertCell(table, "OLD BALANCE",
Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, oldbalance, Element.ALIGN_LEFT,
1, bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "AMT COLLECTED",
Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, amtcollected, Element.ALIGN_LEFT,
1, bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "CURR_OS_BALANCE:",
Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, curosbalance, Element.ALIGN_LEFT,
1, bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "PAYMODE", Element.ALIGN_RIGHT,
1, bfBold12);
insertCell(table, paymode, Element.ALIGN_LEFT, 1,
bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
// tvcuros.setText("" + curosbalance);
// tvoldbalance.setText(oldbalance);
// tvamtcollected.setText(amtcollected);
// tvpaymode.setText(paymode);
// tvpdccheque.setText(pdccheque);
// tvcheque.setText(cheque);
if (bolcheque) {
cheque_layout.setVisibility(View.VISIBLE);
// chequedate =
// getIntent().getExtras().getString("chequedate")
// .toString();
// tvbank.setText(bank);
// tvchequenumber.setText(chequenumber);
// tvchequeamount.setText(chequeamount);
// tvchequedate.setText(chequedate);
insertCell(table, "BANK", Element.ALIGN_RIGHT,
1, bfBold12);
insertCell(table, bank, Element.ALIGN_LEFT, 1,
bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "CHEQUE NO:",
Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, chequenumber,
Element.ALIGN_LEFT, 1, bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "CHEQUE AMOUNT:",
Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, chequeamount,
Element.ALIGN_LEFT, 1, bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
insertCell(table, "CHEQUE DATE:",
Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, chequedate,
Element.ALIGN_LEFT, 1, bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
}
if (bolpdc) {
insertCell(table, "PDC CHEQUE",
Element.ALIGN_RIGHT, 1, bfBold12);
insertCell(table, pdccheque,
Element.ALIGN_LEFT, 1, bfBold12);
// insert an empty row
insertCell(table, "", Element.ALIGN_LEFT, 4,
bfBold12);
}
} else {
Log.d("nzm",
"else for cr4=db.getcusinvaccrevdatas(accrevkey) for "
+ accrevkey);
}
}
// add the PDF table to the paragraph
paragraph.add(table);
// add the paragraph to the document
doc.add(paragraph);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
doc.close();
}
String sdcard = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/noufalpdfdemo";
File dir = new File(sdcard);
// Get the text file
File file = new File(dir, "sample.pdf");
file.canRead();
// FileFinalpath = SdCardpath + "/" + Filepath + Filename;
// File file = new File(FileFinalpath);
if (file.exists()) {
Uri filepath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(filepath, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (Exception e) {
// alert.showAlertDialog(PDF_Activity.this,
// "File Not Started...","File Not Started From SdCard ",
// false);
Log.e("error", "" + e);
}
} else {
// alert.showAlertDialog(PDF_Activity.this,
// "File Not Found...","File Not Found From SdCard ",
// false);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With