使用iText生成PDF文件时出现问题

时间:2019-12-11 17:58:43

标签: java android itext

此代码第一次可以很好地运行,但是当我第二次运行应用程序时,它不会立即生成PDF文件。

 private void savepdf() {
    Document doc=new Document();
    String mfile=new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis());
    String mfilepath= Environment.getExternalStorageDirectory()+"/"+mfile+".pdf";

    Font recipeTitle=new Font(Font.FontFamily.TIMES_ROMAN,20,Font.BOLD);
    Font smallBold=new Font(Font.FontFamily.TIMES_ROMAN,12,Font.BOLD);
    try{
        PdfWriter.getInstance(doc,new FileOutputStream(mfilepath));
        doc.open();
        String mtext=foodTitle.getText().toString();
        String mtext2 = foodIngredient.getText().toString();
        String mtext3 = foodDescription.getText().toString();

        doc.add(new Paragraph("Title:"+ mtext,recipeTitle));
        doc.add(new Paragraph("Ingredient",recipeTitle));
        doc.add(new Paragraph(mtext2,smallBold));
        doc.add(new Paragraph("Description",recipeTitle));
        doc.add(new Paragraph(mtext3,smallBold));
        doc.close();
        Toast.makeText(this, ""+mfile+".pdf"+" is saved to "+mfilepath, Toast.LENGTH_SHORT).show();
    }
    catch (Exception e)
    {
        Toast.makeText(this,"This is Error msg : " +e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试关闭您的文档并捕获异常。我认为程序正在引发异常

 private void savepdf() throws FileNotFoundException, DocumentException {