Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1.  
  2.         // write the document content
  3.         String targetPdf = "/sdcard/pdffromlayout.pdf";
  4.         File filePath;
  5.         filePath = new File(targetPdf);
  6.         try {
  7.             document.writeTo(new FileOutputStream(filePath));
  8.  
  9.          } catch (IOException e) {
  10.             e.printStackTrace();
  11.             Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
  12.         }
  13.  
  14.         // close the document
  15.         document.close();
  16.         Toast.makeText(this, "PDF is created!!!", Toast.LENGTH_SHORT).show();
  17.  
  18.         openGeneratedPDF();
  19.  
  20.     }
  21.  
  22.     private void openGeneratedPDF(){
  23.         File file = new File("/sdcard/pdffromlayout.pdf");
  24.         if (file.exists())
  25.         {
  26.             Intent intent=new Intent(Intent.ACTION_VIEW);
  27.             Uri uri = Uri.fromFile(file);
  28.             intent.setDataAndType(uri, "application/pdf");
  29.             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  30.  
  31.             try
  32.             {
  33.                 startActivity(intent);
  34.             }
  35.             catch(ActivityNotFoundException e)
  36.             {
  37.                 Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
  38.             }
  39.         }
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement