Java 如何将活动保存为包含数据的PDF文件

Java 如何将活动保存为包含数据的PDF文件,java,android,Java,Android,如何将活动及其所有数据以pdf文件的形式保存到手机存储器中 我做了一个cv creator应用程序,我需要用户将最终活动以pdf格式下载到手机上。我能够获得pdf格式的布局,但我丢失了活动中添加的所有数据。需要帮忙吗? 以下是传输到活动的数据的代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.l

如何将活动及其所有数据以pdf文件的形式保存到手机存储器中

我做了一个cv creator应用程序,我需要用户将最终活动以pdf格式下载到手机上。我能够获得pdf格式的布局,但我丢失了活动中添加的所有数据。需要帮忙吗? 以下是传输到活动的数据的代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template1);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        cvPhoto = findViewById(R.id.photo);
        cvName = findViewById(R.id.user_name);
        cvName2 = findViewById(R.id.user_name2);
        cvBirthDate = findViewById(R.id.user_date);
        cvAddress = findViewById(R.id.user_address);
        cvNationality = findViewById(R.id.user_nationality);
        cvPhone = findViewById(R.id.user_contact);
        cvEmail = findViewById(R.id.user_email);
        cvSchool = findViewById(R.id.user_school);
        cvSpecialty = findViewById(R.id.user_specialty);
        cvCertificate = findViewById(R.id.user_certificate);
        cvYear = findViewById(R.id.user_years);
        cvCompany= findViewById(R.id.user_company);
        cvPost = findViewById(R.id.user_post);
        cvDuration= findViewById(R.id.user_duration);
        cvReference= findViewById(R.id.user_reference);
        cvLanguage = findViewById(R.id.user_language);
        cvOthers= findViewById(R.id.user_skill);
        cvDate = findViewById(R.id.current_date);



        Date date = new Date();
        String modifiedDate = new SimpleDateFormat(" dd MMM, yyyy", Locale.getDefault()).format(new Date());
        cvDate.setText(getString(R.string.done_on) + " " + modifiedDate);

        Intent intent = getIntent();
        Bundle bundle = getIntent().getExtras();


        final String tname =bundle.getString("user_name");
        final String tbirtDate =bundle.getString("user_date_of_birth");
        final String  taddress =bundle.getString("user_address");
        final String tnationality =bundle.getString("user_nationality");
        final String temail =bundle.getString("user_email");
        final String tphone =bundle.getString("user_phone");
        final String tschool =bundle.getString("user_school");
        final String tspecialty =bundle.getString("user_specialty");
        final String tcertificate =bundle.getString("user_certificate");
        final String tyears =bundle.getString("user_years");
        final String tcompany =bundle.getString("user_company");
        final String tpost =bundle.getString("user_post");
        final String tduration =bundle.getString("user_duration");
        final String treference =bundle.getString("user_reference");
        final String tlanguages =bundle.getString("user_languages");
        final String totherSkills =bundle.getString("user_other_skills");

        Bitmap resized = intent.getParcelableExtra("BitmapImage");

        cvPhoto.setImageBitmap(resized);

        cvName.setText(getString(R.string.full_name) +" "+ tname);
        cvBirthDate.setText(getString(R.string.date_of_birth) +" "+ tbirtDate);
        cvAddress.setText(getString(R.string.address) +" "+ taddress);
        cvNationality.setText(getString(R.string.nationality) +" "+ tnationality);
        cvEmail.setText(getString(R.string.email) +" "+ temail);
        cvPhone.setText(getString(R.string.phone_number) +" "+ tphone);
        cvSchool.setText(getString(R.string.school_attended) +"\n "+ tschool);
        cvSpecialty.setText(getString(R.string.specialty_course) +"\n "+ tspecialty);
        cvCertificate.setText(getString(R.string.certificates_obtained) +"\n "+ tcertificate);
        cvYear.setText(getString(R.string.years) +"\n "+ tyears);
        cvCompany.setText(getString(R.string.company_organisation_institution) +"\n "+ tcompany);
        cvPost.setText(getString(R.string.phone_number) +"\n "+ tpost);
        cvDuration.setText(getString(R.string.duration) +"\n "+ tduration);
        cvReference.setText(getString(R.string.reference) +"\n "+ treference);
        cvLanguage.setText(tlanguages);
        cvOthers.setText(totherSkills);
        cvName2.setText(tname);
   FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                    PdfDocument document = new PdfDocument();

                PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();

                // start a page

                PdfDocument.Page page = document.startPage(pageInfo);


                // add something on the page
                LayoutInflater inflater = (LayoutInflater)
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View content = inflater.inflate(R.layout.activity_template1, null);

                content.measure(1400, 2250);
                content.layout(0,0, 2250, 1400);


                content.draw(page.getCanvas());

                // finish the page
                document.finishPage(page);
                // add more pages

                // write the document content
                String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
                File file = new File(directory_path);
                if (!file.exists()) {
                    file.mkdirs();
                }
                String targetPdf = directory_path+"My CV.pdf";
                File filePath = new File(targetPdf);
                try {
                    document.writeTo(new FileOutputStream(filePath));
                    Toast.makeText(Template1Activity.this, "Done", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Log.e("main", "error "+e.toString());
                    Toast.makeText(Template1Activity.this, "Something wrong: " + e.toString(),  Toast.LENGTH_LONG).show();
                }
                // close the document
                document.close();
下面是创建带有活动的pdf文档的代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_template1);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        cvPhoto = findViewById(R.id.photo);
        cvName = findViewById(R.id.user_name);
        cvName2 = findViewById(R.id.user_name2);
        cvBirthDate = findViewById(R.id.user_date);
        cvAddress = findViewById(R.id.user_address);
        cvNationality = findViewById(R.id.user_nationality);
        cvPhone = findViewById(R.id.user_contact);
        cvEmail = findViewById(R.id.user_email);
        cvSchool = findViewById(R.id.user_school);
        cvSpecialty = findViewById(R.id.user_specialty);
        cvCertificate = findViewById(R.id.user_certificate);
        cvYear = findViewById(R.id.user_years);
        cvCompany= findViewById(R.id.user_company);
        cvPost = findViewById(R.id.user_post);
        cvDuration= findViewById(R.id.user_duration);
        cvReference= findViewById(R.id.user_reference);
        cvLanguage = findViewById(R.id.user_language);
        cvOthers= findViewById(R.id.user_skill);
        cvDate = findViewById(R.id.current_date);



        Date date = new Date();
        String modifiedDate = new SimpleDateFormat(" dd MMM, yyyy", Locale.getDefault()).format(new Date());
        cvDate.setText(getString(R.string.done_on) + " " + modifiedDate);

        Intent intent = getIntent();
        Bundle bundle = getIntent().getExtras();


        final String tname =bundle.getString("user_name");
        final String tbirtDate =bundle.getString("user_date_of_birth");
        final String  taddress =bundle.getString("user_address");
        final String tnationality =bundle.getString("user_nationality");
        final String temail =bundle.getString("user_email");
        final String tphone =bundle.getString("user_phone");
        final String tschool =bundle.getString("user_school");
        final String tspecialty =bundle.getString("user_specialty");
        final String tcertificate =bundle.getString("user_certificate");
        final String tyears =bundle.getString("user_years");
        final String tcompany =bundle.getString("user_company");
        final String tpost =bundle.getString("user_post");
        final String tduration =bundle.getString("user_duration");
        final String treference =bundle.getString("user_reference");
        final String tlanguages =bundle.getString("user_languages");
        final String totherSkills =bundle.getString("user_other_skills");

        Bitmap resized = intent.getParcelableExtra("BitmapImage");

        cvPhoto.setImageBitmap(resized);

        cvName.setText(getString(R.string.full_name) +" "+ tname);
        cvBirthDate.setText(getString(R.string.date_of_birth) +" "+ tbirtDate);
        cvAddress.setText(getString(R.string.address) +" "+ taddress);
        cvNationality.setText(getString(R.string.nationality) +" "+ tnationality);
        cvEmail.setText(getString(R.string.email) +" "+ temail);
        cvPhone.setText(getString(R.string.phone_number) +" "+ tphone);
        cvSchool.setText(getString(R.string.school_attended) +"\n "+ tschool);
        cvSpecialty.setText(getString(R.string.specialty_course) +"\n "+ tspecialty);
        cvCertificate.setText(getString(R.string.certificates_obtained) +"\n "+ tcertificate);
        cvYear.setText(getString(R.string.years) +"\n "+ tyears);
        cvCompany.setText(getString(R.string.company_organisation_institution) +"\n "+ tcompany);
        cvPost.setText(getString(R.string.phone_number) +"\n "+ tpost);
        cvDuration.setText(getString(R.string.duration) +"\n "+ tduration);
        cvReference.setText(getString(R.string.reference) +"\n "+ treference);
        cvLanguage.setText(tlanguages);
        cvOthers.setText(totherSkills);
        cvName2.setText(tname);
   FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                    PdfDocument document = new PdfDocument();

                PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();

                // start a page

                PdfDocument.Page page = document.startPage(pageInfo);


                // add something on the page
                LayoutInflater inflater = (LayoutInflater)
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View content = inflater.inflate(R.layout.activity_template1, null);

                content.measure(1400, 2250);
                content.layout(0,0, 2250, 1400);


                content.draw(page.getCanvas());

                // finish the page
                document.finishPage(page);
                // add more pages

                // write the document content
                String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
                File file = new File(directory_path);
                if (!file.exists()) {
                    file.mkdirs();
                }
                String targetPdf = directory_path+"My CV.pdf";
                File filePath = new File(targetPdf);
                try {
                    document.writeTo(new FileOutputStream(filePath));
                    Toast.makeText(Template1Activity.this, "Done", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Log.e("main", "error "+e.toString());
                    Toast.makeText(Template1Activity.this, "Something wrong: " + e.toString(),  Toast.LENGTH_LONG).show();
                }
                // close the document
                document.close();
最终结果只给出了没有数据的活动。

最好使用例如,并以DIN A4或US字母格式呈现简历

否则那将是一个PDF,里面有一个屏幕截图,这不是机器可读的

如果有标准化的简历XML格式,PDF/A-3也可能是一个选项。

更好地使用例如,并以DIN A4或美国字母格式呈现简历

否则那将是一个PDF,里面有一个屏幕截图,这不是机器可读的


如果有标准化的CV XML格式,PDF/A-3也可能是一个选项。

因为您还没有指定生成PDF的方式,所以可以查看android sdk附带的类

确保您拥有android.permission.WRITE\u EXTERNAL\u STORAGE权限

将父视图传递给
createPdfFromView

 private void createPdfFromView(View view, String fileName, int pageWidth, int pageHeight, int pageNumber) {

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        File file = new File(path, fileName.concat(".pdf"));

        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (file.exists()) {
            PdfDocument document = new PdfDocument();
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, pageNumber).create();
            PdfDocument.Page page = document.startPage(pageInfo);

            view.draw(page.getCanvas());

            document.finishPage(page);

            try {
                Toast.makeText(this, "Saving...", Toast.LENGTH_SHORT).show();
                document.writeTo(fOut);
            } catch (IOException e) {
                Toast.makeText(this, "Failed...", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

            document.close();

            /*Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);*/

        } else {
            //..
        }

    }

由于您尚未指定如何生成pdf,所以可以查看android sdk附带的类

确保您拥有android.permission.WRITE\u EXTERNAL\u STORAGE权限

将父视图传递给
createPdfFromView

 private void createPdfFromView(View view, String fileName, int pageWidth, int pageHeight, int pageNumber) {

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        File file = new File(path, fileName.concat(".pdf"));

        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (file.exists()) {
            PdfDocument document = new PdfDocument();
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, pageNumber).create();
            PdfDocument.Page page = document.startPage(pageInfo);

            view.draw(page.getCanvas());

            document.finishPage(page);

            try {
                Toast.makeText(this, "Saving...", Toast.LENGTH_SHORT).show();
                document.writeTo(fOut);
            } catch (IOException e) {
                Toast.makeText(this, "Failed...", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

            document.close();

            /*Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);*/

        } else {
            //..
        }

    }

是否将用户界面导出为pdf?[是]@shbas图像或文本?是否将用户界面导出为pdf?[是]@shbas图像或文本?