You can play around with both and see if droidText can work for you. iText for Android provides you with a 30 day trial license, just use the link here http://demo.itextsupport.com/newslicense/
Now for the purpose of printing to a bluetooth printer this application uses PrintShare Premium Key app which is also a licensed product. Here is the link for the google play store
https://play.google.com/store/apps/details?id=com.dynamixsoftware.printershare.premium
(Please Note: This is not a recommendation to use the above mentioned product, you can find another app on the play store or write to the bluetooth printer directly)
If you get the error Could not find class 'org.spongycastle.cert.X509CertificateHolder' during run time then you must include the following libraries from the spongycastle github project, here is the link https://github.com/rtyley/spongycastle-eclipse/tree/master/libs
- sc-light-jdk15on-1.47.0.2.jar
- scpkix-jdk15on-1.47.0.2.jar
- scprov-jdk15on-1.47.0.2.jar
![]() ![]() |
![]() ![]() |
Screen Layout - activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <? xml version = "1.0" encoding = "UTF-8" ?> android:layout_height = "match_parent" tools:context = ".MainActivity" android:padding = "5dp" > < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentLeft = "true" android:layout_alignParentTop = "true" android:text = "Enter you name and hit PRINT" android:textAppearance = "?android:attr/textAppearanceMedium" /> < EditText android:id = "@+id/preparedBy" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@+id/textView1" android:layout_below = "@+id/textView1" android:ems = "10" android:inputType = "textPersonName" > < requestFocus /> </ EditText > < Button android:id = "@+id/printPDF" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignRight = "@+id/preparedBy" android:layout_below = "@+id/preparedBy" android:layout_marginTop = "10dp" android:text = "Print PDF" android:onClick = "printPDF" /> </ RelativeLayout > |
Activity source - MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | package com.as400samplecode; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import com.itextpdf.license.LicenseKey; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private static final String LOG_TAG = "GeneratePDF" ; private EditText preparedBy; private File pdfFile; private String filename = "Sample.pdf" ; private String filepath = "MyInvoices" ; private BaseFont bfBold; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); //get reference to the edittext so pull data out preparedBy = (EditText) findViewById(R.id.preparedBy); //need to load license from the raw resources for iText //skip this if you are going to use droidText InputStream license = this .getResources().openRawResource(R.raw.itextkey); LicenseKey.loadLicenseFile(license); //check if external storage is available so that we can dump our PDF file there if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { Log.v(LOG_TAG, "External Storage not available or you don't have permission to write" ); } else { //path for the PDF file in the external storage pdfFile = new File(getExternalFilesDir(filepath), filename); } } public void printPDF(View v) { switch (v.getId()) { //start the process of creating the PDF and then print it case R.id.printPDF: String personName = preparedBy.getText().toString(); generatePDF(personName); break ; } } private void generatePDF(String personName){ //create a new document Document document = new Document(); try { PdfWriter docWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile)); document.open(); PdfContentByte cb = docWriter.getDirectContent(); //initialize fonts for text printing initializeFonts(); //the company logo is stored in the assets which is read only //get the logo and print on the document InputStream inputStream = getAssets().open( "olympic_logo.png" ); Bitmap bmp = BitmapFactory.decodeStream(inputStream); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100 , stream); Image companyLogo = Image.getInstance(stream.toByteArray()); companyLogo.setAbsolutePosition( 25 , 700 ); companyLogo.scalePercent( 25 ); document.add(companyLogo); //creating a sample invoice with some customer data createHeadings(cb, 400 , 780 , "Company Name" ); createHeadings(cb, 400 , 765 , "Address Line 1" ); createHeadings(cb, 400 , 750 , "Address Line 2" ); createHeadings(cb, 400 , 735 , "City, State - ZipCode" ); createHeadings(cb, 400 , 720 , "Country" ); //list all the products sold to the customer float [] columnWidths = { 1 .5f, 2f, 5f, 2f,2f}; //create PDF table with the given widths PdfPTable table = new PdfPTable(columnWidths); // set table width a percentage of the page width table.setTotalWidth(500f); PdfPCell cell = new PdfPCell( new Phrase( "Qty" )); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell(cell); cell = new PdfPCell( new Phrase( "Item Number" )); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); cell = new PdfPCell( new Phrase( "Item Description" )); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); cell = new PdfPCell( new Phrase( "Price" )); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell(cell); cell = new PdfPCell( new Phrase( "Ext Price" )); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell(cell); table.setHeaderRows( 1 ); DecimalFormat df = new DecimalFormat( "0.00" ); for ( int i= 0 ; i < 15 ; i++ ){ double price = Double.valueOf(df.format(Math.random() * 10 )); double extPrice = price * (i+ 1 ) ; table.addCell(String.valueOf(i+ 1 )); table.addCell( "ITEM" + String.valueOf(i+ 1 )); table.addCell( "Product Description - SIZE " + String.valueOf(i+ 1 )); table.addCell(df.format(price)); table.addCell(df.format(extPrice)); } //absolute location to print the PDF table from table.writeSelectedRows( 0 , - 1 , document.leftMargin(), 650 , docWriter.getDirectContent()); //print the signature image along with the persons name inputStream = getAssets().open( "signature.png" ); bmp = BitmapFactory.decodeStream(inputStream); stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100 , stream); Image signature = Image.getInstance(stream.toByteArray()); signature.setAbsolutePosition(400f, 150f); signature.scalePercent(25f); document.add(signature); createHeadings(cb, 450 , 135 ,personName); document.close(); } catch (Exception e){ e.printStackTrace(); } //PDF file is now ready to be sent to the bluetooth printer using PrintShare Intent i = new Intent(Intent.ACTION_VIEW); i.setPackage( "com.dynamixsoftware.printershare" ); i.setDataAndType(Uri.fromFile(pdfFile), "application/pdf" ); startActivity(i); } private void createHeadings(PdfContentByte cb, float x, float y, String text){ cb.beginText(); cb.setFontAndSize(bfBold, 8 ); cb.setTextMatrix(x,y); cb.showText(text.trim()); cb.endText(); } private static boolean isExternalStorageReadOnly() { String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { return true ; } return false ; } private static boolean isExternalStorageAvailable() { String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { return true ; } return false ; } private void initializeFonts(){ try { bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true ; } } |
1 comment:
good post.it helped me a lot.
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.