0

This is my activity and I want to save this activity as pdf or image can any one help me out to solve this problem I have tried save as pdf code but it generate empty pdf file and the activity code of which I want to generate PDF file is given below

package com.example.conceptsimilarity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.chaquo.python.PyObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class result extends AppCompatActivity {
 TextView txtwords,txtspellmistake,txtper,txtper2,txtstop,txtwordper,totnumber,obtainnumber,spell,totalnumberofsenindoc,gr;
 Button savefile;
    @SuppressLint("ResourceType")
    @RequiresApi(api = Build.VERSION_CODES.M)
   // @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        txtwords=findViewById(R.id.textnowords);
        txtspellmistake=findViewById(R.id.textspell);
        txtper=findViewById(R.id.textpercentage);
        txtper2=findViewById(R.id.textpercentage2);
        txtstop=findViewById(R.id.textpercentage4);
        txtwordper=findViewById(R.id.textpercentage3);
        totnumber=findViewById(R.id.total);
        obtainnumber=findViewById(R.id.obtain);
        spell=findViewById(R.id.spellingmistake);
        savefile=findViewById(R.id.save);
        totalnumberofsenindoc=findViewById(R.id.totalnuberofsentencesindoc);
        gr=findViewById(R.id.grade);

        String sent;
        int wordsixty;
         int sentthirty, grammermistakes,spellmistakeinword,word = 0,sentence=0;
        int perword=0;
        int persent = 0;
        int i=100;
        int toalnumberindoc=0;
        int limitindoc=0;


        Intent intent=getIntent();
        String obje=intent.getStringExtra("numberwords");
        String grammerchecker=intent.getStringExtra("spellmistake");
        String wordsimilaritys=intent.getStringExtra("wordsimilarity");
        String countstop=intent.getStringExtra("stop");
        String sentence_count=intent.getStringExtra("sentence_count");
        String dfsimilarity=intent.getStringExtra("defitionsimilarity");
        String docsentencecount=intent.getStringExtra("docsentcount");
        String spellmistake=intent.getStringExtra("wordspelling");
        String docnumberwords=intent.getStringExtra("filenumberofwords");

        //word similarity percentage is in dbw
        String dbw=intent.getStringExtra("datacount");
        String totalnum=intent.getStringExtra("totalnumber");
        float word_value=Integer.parseInt(wordsimilaritys);
        float word_count=Integer.parseInt(countstop);
        float def_value=Integer.parseInt(dfsimilarity);
        float sent_count=Integer.parseInt(sentence_count);
        int spellint=Integer.parseInt(spellmistake);
        int grammerint=Integer.parseInt(grammerchecker);
        int totalnumber=Integer.parseInt(totalnum);
        float percentageofwords=Float.parseFloat(dbw);
        toalnumberindoc=Integer.parseInt(obje);
        limitindoc=Integer.parseInt(docnumberwords);

        double sentence_percentage = (double) Math.round((def_value/sent_count)*100);
        if (sentence_percentage>100){
            sentence_percentage=100;
        }


        sent=Double.toString(sentence_percentage);

        wordsixty= (int ) (percentageofwords*60)/i;
        sentthirty=(int) ((sentence_percentage*40)/i);
        //spelling mistakes (cuting from word percentage)
        //grammer mistakes (cutting from sentences)
        //grammar*100/total sent

        grammermistakes=  (grammerint*40/Integer.parseInt(docsentencecount));
        spellmistakeinword= (int) (spellint*60/word_count);

        Toast.makeText(this,""+docsentencecount+"",Toast.LENGTH_LONG).show();


        if (wordsixty!=0||sentthirty!=0){
             perword= (int) (percentageofwords-spellmistakeinword);
             persent= (int) (sentence_percentage-grammermistakes);
            word= (wordsixty-spellmistakeinword);
            sentence=(sentthirty- grammermistakes);
        }

               // Toast.makeText(this,""+wordsimilaritys,Toast.LENGTH_LONG).show();

        float scorefinal=((word+sentence)*totalnumber)/100;
        totnumber.setText(totalnum);

        float gradepercentage=0;

        if(toalnumberindoc >limitindoc)
        {
            txtwords.setText(obje);
            txtwords.setTextColor(getResources().getColor(R.color.Red));
        }
        else {
            txtwords.setText(obje);
        }

        gradepercentage=scorefinal*100/totalnumber;

        if(gradepercentage>=90)
        {
            gr.setText("A+");
        }
        else if(gradepercentage>=80 && gradepercentage<90)
        {
            gr.setText("A");
        }
        else if(gradepercentage>=70 && gradepercentage<80)
        {
            gr.setText("B");
        }
        else if(gradepercentage>=60 && gradepercentage<70)
        {
            gr.setText("C");
        }
        else if(gradepercentage>=50 && gradepercentage<60)
        {
            gr.setText("D");
        }
        else
        {
            gr.setText("F");
            gr.setTextColor(getResources().getColor(R.color.Red));
        }



        txtspellmistake.setText(grammerchecker);
        txtstop.setText(countstop);
        txtper.setText(wordsimilaritys);
        txtper2.setText(Integer.toString(persent)+"%");
        txtwordper.setText(Integer.toString(perword)+"%");
        obtainnumber.setText(Float.toString( scorefinal));
        spell.setText(Integer.toString(spellint));
        totalnumberofsenindoc.setText(docsentencecount);
//       Toast.makeText(this," "+totalnum,Toast.LENGTH_LONG).show();

    }

    @Override
    public void onBackPressed() {
        finish();
        startActivity(new Intent(this, documentupload.class));
    }

   @RequiresApi(api = Build.VERSION_CODES.KITKAT)
   private void createpdf()
   {

           PdfDocument myPdfDocument=new PdfDocument();
               Paint myPaint=new Paint();
               PdfDocument.PageInfo myPageInfo1=new PdfDocument.PageInfo.Builder(1200,2010,1).create();
               PdfDocument.Page myPage1=myPdfDocument.startPage(myPageInfo1);
               Canvas canvas=myPage1.getCanvas();

               myPdfDocument.finishPage(myPage1);

               File file=new File(Environment.getExternalStorageDirectory(),"result.pdf");
               try {
                   myPdfDocument.writeTo(new FileOutputStream(file));
               }catch (IOException e){
                   e.printStackTrace();
               }
               myPdfDocument.close();
               Toast.makeText(result.this,"Save "+file,Toast.LENGTH_SHORT).show();

           }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void save(View view) {
        createpdf();
    }

I have create a method createpdf() in the activity but it did not work and this activity is a scroll activity the screen shot of activity will be under

This is the activity and i want to save this activity data as pdf

This is the activity and i want to save this activity data as pdf

this activity is a scroll activity this is remaining part

this activity is a scroll activity this is remaining part

This is the pdf file which is generated but it is empty

This is the pdf file which is generated but it is empty

Noman Omer
  • 53
  • 4

0 Answers0