Wednesday, December 13, 2017

Save the File in PDF and View PDF Android Exmaple

 private void saveDataInPDF(List<ContactDetail> calllist, String filename) {
        Document doc = new Document();

        try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";

            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();

            File file = new File(dir,filename);
            FileOutputStream fOut = new FileOutputStream(file);

            PdfWriter.getInstance(doc, fOut);

            //open the document
            doc.open();
            Paragraph p1=null;
            for(int i=0;i<calllist.size();i++) {
                ContactDetail contactDetail=calllist.get(i);
                p1 = new Paragraph(contactDetail.getCalltype()+""+contactDetail.getMobilenumber());

                doc.add(p1);
            }

        } catch (DocumentException de) {
            Log.e("PDFCreator", "DocumentException:" + de);
        } catch (IOException e) {
            Log.e("PDFCreator", "ioException:" + e);
        } finally {
            doc.close();
        }

        viewPdf(filename, "Dir");

    }
//////////////////////viewpdf


    private void viewPdf(String file, String directory) {

        try {
            File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file);
            Uri path = Uri.fromFile(pdfFile);
            Log.e("Path", path.toString());

            Intent objIntent = new Intent(Intent.ACTION_VIEW);
            objIntent.setDataAndType(path, "application/pdf");
            objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(objIntent);
            Intent email = new Intent(Intent.ACTION_SEND);
             email.putExtra(Intent.EXTRA_SUBJECT, directory);
              email.putExtra(Intent.EXTRA_TEXT, file);
              email.setType("application/pdf");
              email.putExtra(Intent.EXTRA_STREAM, path);
              startActivity(Intent.createChooser(email, "Send Mail..."));
            //sendMail(path);





        }
        catch (ActivityNotFoundException e) {
            Toast.makeText(ContactRestore.this, "Can't Read Pdf File", Toast.LENGTH_SHORT).show();
        }
    }

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...