我正在尝试在我的应用程序中创建一个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, 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(); } }
PdfDocument类允许从本机Android内容生成PDF文档.通过使用这个类,我们可以创建pdf并使用PdfRenderer打开它.用于创建pdf文件的示例代码
public void stringtopdf(String data) { String extstoragedir = Environment.getExternalStorageDirectory().toString(); File fol = new File(extstoragedir, "pdf"); File folder=new File(fol,"pdf"); if(!folder.exists()) { boolean bool = folder.mkdir(); } try { final File file = new File(folder, "sample.pdf"); file.createNewFile(); FileOutputStream fOut = new FileOutputStream(file); PdfDocument document = new PdfDocument(); PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(100, 100, 1).create(); PdfDocument.Page page = document.startPage(pageInfo); Canvas canvas = page.getCanvas(); Paint paint = new Paint(); canvas.drawText(data, 10, 10, paint); document.finishPage(page); document.writeTo(fOut); document.close(); }catch (IOException e){ Log.i("error",e.getLocalizedMessage()); }