【问题标题】:Problem generating a PDF file using iText使用 iText 生成 PDF 文件的问题
【发布时间】:2019-12-11 17:58:43
【问题描述】:

此代码第一次可以运行良好,但是当我第二次运行应用程序时,它不会立即生成 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();
    }
}

【问题讨论】:

    标签: java android itext


    【解决方案1】:

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

     private void savepdf() throws FileNotFoundException, DocumentException {
    

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      相关资源
      最近更新 更多