将活动另存为 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() 方法,但它不起作用,并且此活动是滚动活动,活动的屏幕截图将位于

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

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

this activity is a scroll activity this is remaining part

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

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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...