Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // write the document content
- String targetPdf = "/sdcard/pdffromlayout.pdf";
- File filePath;
- filePath = new File(targetPdf);
- try {
- document.writeTo(new FileOutputStream(filePath));
- } catch (IOException e) {
- e.printStackTrace();
- Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
- }
- // close the document
- document.close();
- Toast.makeText(this, "PDF is created!!!", Toast.LENGTH_SHORT).show();
- openGeneratedPDF();
- }
- private void openGeneratedPDF(){
- File file = new File("/sdcard/pdffromlayout.pdf");
- if (file.exists())
- {
- Intent intent=new Intent(Intent.ACTION_VIEW);
- Uri uri = Uri.fromFile(file);
- intent.setDataAndType(uri, "application/pdf");
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- try
- {
- startActivity(intent);
- }
- catch(ActivityNotFoundException e)
- {
- Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement