0

For this, I fetched all data in a activity which is stored in sqlite. Now all data should be converted into pdf form and automatically should be attach on email. When I press share button. Please give me your valuable suggestions, links or complete source code if any one have.

public class ListofShopping extends Activity {


    TextView cat, prd, iName, iBrand, iAge, iColor, iQnty, iCst, iDsc;
    Button discard, share;
    String s1, s2, s3, s4, s5, s6, s7, s8, s9, a1, a2;
    SQLiteDatabase db;
    Cursor c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listofshop);


        cat     = (TextView) findViewById(R.id.eCat);
        prd     = (TextView) findViewById(R.id.ePrdct);
        iName   = (TextView) findViewById(R.id.eItem);
        iBrand  = (TextView) findViewById(R.id.eBrand);
        iAge    = (TextView) findViewById(R.id.eAge);
        iColor  = (TextView) findViewById(R.id.eClr);
        iQnty   = (TextView) findViewById(R.id.eQnty);
        iCst    = (TextView) findViewById(R.id.eCost);
        iDsc    = (TextView) findViewById(R.id.eDsc);
        discard = (Button)   findViewById(R.id.dscrd);
        share   = (Button)   findViewById(R.id.email);

        db = openOrCreateDatabase("Shopping.db", MODE_PRIVATE, null);
        c = db.rawQuery("select * from SHOPPING_LIST", null);
        if(c.getCount()==0){

        }
        while(c.moveToNext()){
            s1= c.getString(0);
            s2= c.getString(1);
            s3= c.getString(2);
            s4= c.getString(3);
            s5= c.getString(4);
            s6= c.getString(5);
            s7= c.getString(6);
            s8= c.getString(7);
            s9= c.getString(8);
            s10=c.getString(9);
            s11=c.getString(10);
        }
        c.close();
        db.close();
        cat.setText(s1);
        prd.setText(s2);
        iName.setText(s3);
        iBrand.setText(s4);
        iAge.setText(s5);
        iColor.setText(s6);
        iQnty.setText(s7);
        iCst.setText(s8);
        iDsc.setText(s9);

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Where exactly you are getting issue? To generate pdf or to send it as an attachment via email? – sUndeep Feb 10 '15 at 06:43
  • hi sundeep.. thank you for quick reply.. Actually i am new in android, Currently my requirement is to get data from sqlite and convert it to pdf and when i click on share button that converted pdf file should be automatically attached in email please i dot know how to perform these two task – Ahana Srivastava Feb 10 '15 at 06:53

2 Answers2

0

See there are various libraries available which you can use to generate PDF. Here is the link: itext pdf generator is an open source library for android?

I personally used iText library once in a project. You can choose any one of them. Now come to the part how can you share via email. You can get the answer on below links:

Attaching file in email

Send a PDF file as Mail or offer app to directly view the file

In case if you still have any problem, please let me know.

Community
  • 1
  • 1
sUndeep
  • 362
  • 3
  • 16
  • Dear Sundeep.. then a lot for helping but what about DroidTtext..? Had you ever used it ? – Ahana Srivastava Feb 10 '15 at 07:02
  • Most welcome. Please mark my answer as resolved so that it will be helpful for other beginners. No. Never got a chance to look at DroidText. You please give a try to implement that. – sUndeep Feb 10 '15 at 07:07
  • if you have any complete source code then please let me know.. mail to me ahana000in@gmail.com... i will wait from your side for your reply.. I need this example very badly, because i am sucked in my project – Ahana Srivastava Feb 10 '15 at 07:12
  • Sorry I did it many time back. No such ready made code snippet with me now!! – sUndeep Feb 10 '15 at 08:33
0

It is so simple to create the pdf but dont know about attach the pdf file in mail

First download the droidText.0.2.jar and add in Gradle file if you are using Android-Studio. Then write this code for the PDF:

public void createPDF() {
    Document doc = new Document();


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

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

        Log.d("PDFCreator", "PDF Path: " + path);

        //This is for random name 
        number = new ArrayList<Integer>();
        for (int i = 1; i <= 10; ++i) number.add(i);
        Collections.shuffle(number);

        File file = new File(dir, "Document" + number + ".pdf");
        FileOutputStream fOut = new FileOutputStream(file);
        PdfWriter.getInstance(doc, fOut);

        //open the document
        doc.open();

        Paragraph p1 = new Paragraph("TENANTS : " + tenants.getText().toString());
        Font paraFont = new Font(Font.COURIER);
        p1.setAlignment(Paragraph.ALIGN_CENTER);
        p1.setFont(paraFont);

        //add paragraph to document
        doc.add(p1);

        Paragraph p2 = new Paragraph("OFFFER NUMBER : " + offer_number.getText().toString());
        Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
        p2.setAlignment(Paragraph.ALIGN_CENTER);
        p2.setFont(paraFont2);

        doc.add(p2);

        Paragraph p3 = new Paragraph("OFFFER NUMBER : " + offer_number.getText().toString());
        Font paraFont3 = new Font(Font.COURIER, 14.0f, Color.GREEN);
        p3.setAlignment(Paragraph.ALIGN_CENTER);
        p3.setFont(paraFont2);

        doc.add(p3);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.logo);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        Image myImg = Image.getInstance(stream.toByteArray());
        myImg.setAlignment(Image.MIDDLE);

//            add image to document
        doc.add(myImg);

        //set footer
        Phrase footerText = new Phrase("ADUREC DOCUMENT");
        HeaderFooter pdfFooter = new HeaderFooter(footerText, true);
        doc.setFooter(pdfFooter);

//            Toast.makeText(getApplicationContext(), "success pdf", Toast
//                    .LENGTH_LONG).show();

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

}

and just call this method

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67