以编程方式在android中创建pdf文件并在其中编写

以编程方式在android中创建pdf文件并在其中编写,第1张

概述我正在尝试在我的应用程序中创建一个pdf文件,将其保存在打开它的外部存储器上.保存文件对我来说不是问题,也不是打开文件,我的问题是创建文件并写入文件.所以在网上进行一些研究后,我发现了以下方法: File file = new File(directoryName, fileName); // Creating output stream to write in the newl 我正在尝试在我的应用程序中创建一个pdf文件,将其保存在打开它的外部存储器上.保存文件对我来说不是问题,也不是打开文件,我的问题是创建文件并写入文件.所以在网上进行一些研究后,我发现了以下方法:
file file = new file(directoryname,filename);        // Creating output stream to write in the newly created file        fileOutputStream fOut = null;        try {            fOut = new fileOutputStream(file);        } catch (fileNotFoundException e) {            e.printstacktrace();        }        // Creating a new document        document document = new document(PageSize.A4,50,50);        try {            pdfWriter.getInstance(document,fOut);            // Open the document for writing            document.open();            // Write in the document            document.add(new Paragraph("Hello world"));            document.close();        } catch(documentException de) {            System.err.println(de.getMessage());        }

运行我的应用程序并执行上面的代码后,我收到以下错误:

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/color;

有人会知道我的代码有什么问题,或者其他确保用于创建和编写pdf文件的方法有什么问题?

谢谢 !

解决方法 显然我使用的代码与androID不兼容,因此我得到了错误.您可以在下面找到实际正常工作的正确代码(用于创建pdf文件,在其中放入一些内容,保存并打开新创建的文件):

PS:为此,您需要将iTextG的jar添加到您的项目中:

// Method for creating a pdf file from text,saving it then opening it for display    public voID createanddisplaypdf(String text) {        document doc = new document();        try {            String path = Environment.getExternalStorageDirectory().getabsolutePath() + "/Dir";            file dir = new file(path);            if(!dir.exists())                dir.mkdirs();            file file = new file(dir,"newfile.pdf");            fileOutputStream fOut = new fileOutputStream(file);            pdfWriter.getInstance(doc,fOut);            //open the document            doc.open();            Paragraph p1 = new Paragraph(text);            Font paraFont= new Font(Font.COURIER);            p1.setAlignment(Paragraph.AliGN_CENTER);            p1.setFont(paraFont);            //add paragraph to document            doc.add(p1);            } catch (documentException de) {            Log.e("Pdfcreator","documentException:" + de);        } catch (IOException e) {            Log.e("Pdfcreator","ioException:" + e);        }        finally {            doc.close();        }        vIEwpdf("newfile.pdf","Dir");    }    // Method for opening a pdf file    private voID vIEwpdf(String file,String directory) {        file pdffile = new file(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file);        Uri path = Uri.fromfile(pdffile);        // Setting the intent for pdf reader        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);        pdfIntent.setDataAndType(path,"application/pdf");        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_top);        try {            startActivity(pdfIntent);        } catch (ActivityNotFoundException e) {            Toast.makeText(tableActivity.this,"Can't read pdf file",Toast.LENGTH_SHORT).show();        }    }
总结

以上是内存溢出为你收集整理的以编程方式在android中创建pdf文件并在其中编写全部内容,希望文章能够帮你解决以编程方式在android中创建pdf文件并在其中编写所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1138015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存