首页 > 解决方案 > 将活动另存为 PDF,但会生成空的 pdf 文件

问题描述

这是我的活动,我想将此活动另存为 pdf 或图像,任何人都可以帮助我解决这个问题我尝试另存为 pdf 代码,但它生成空的 pdf 文件和我想要生成 PDF 文件的活动代码下面给出

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();
    }

我在活动中创建了一个方法 createpdf() 但它不起作用,而且这个活动是一个滚动活动,活动的屏幕截图将在下面

这是活动,我想将此活动数据保存为 pdf

这是活动,我想将此活动数据保存为 pdf

此活动是滚动活动这是剩余部分

此活动是滚动活动这是剩余部分

这是生成的pdf文件,但它是空的

这是生成的pdf文件,但它是空的

标签: javaandroidpdfpdf-generationpdfdocument

解决方案


推荐阅读