使用iText生成PDF文件的问题

问题描述 投票:0回答:1

此代码第一次可以很好地运行,但是当我第二次运行应用程序时,它不会立即生成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个回答
0
投票

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

 private void savepdf() throws FileNotFoundException, DocumentException {
© www.soinside.com 2019 - 2024. All rights reserved.