Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print PDF using Android 4.4 Printing framework [closed]

How to print already downloaded PDF using Android 4.4 printing framework?

I viewed the developer documentation. but no luck. Any example would be helpful

like image 956
Dinesh T A Avatar asked Dec 21 '13 08:12

Dinesh T A


People also ask

How do I Print a PDF app?

Steps for Printing to PDF on Android DevicesChoose the menu option (the icon with three dots located on the right-hand corner of your Android device). Click "Save as PDF". Select the printer that you want to use for printing. If you can't locate it, click all printers to see the list of all connected printers.


1 Answers

After spend some hours on google i found the solution.

PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE); String jobName = this.getString(R.string.app_name) + " Document"; printManager.print(jobName, pda, null);  PrintDocumentAdapter pda = new PrintDocumentAdapter(){      @Override     public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){         InputStream input = null;         OutputStream output = null;          try {              input = new FileInputStream(file to print);             output = new FileOutputStream(destination.getFileDescriptor());              byte[] buf = new byte[1024];             int bytesRead;              while ((bytesRead = input.read(buf)) > 0) {                  output.write(buf, 0, bytesRead);             }              callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});          } catch (FileNotFoundException ee){             //Catch exception         } catch (Exception e) {             //Catch exception         } finally {             try {                 input.close();                 output.close();             } catch (IOException e) {                 e.printStackTrace();             }         }     }      @Override     public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras){          if (cancellationSignal.isCanceled()) {             callback.onLayoutCancelled();             return;         }           PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();          callback.onLayoutFinished(pdi, true);     } }; 
like image 102
Dinesh T A Avatar answered Oct 16 '22 12:10

Dinesh T A