在 Android Studio 中使用 iText 以 PDF 格式显示数据库

Displaying a database in PDF using iText in Android Studio

我有一个函数可以创建一个显示数据库 (SQLite) 的 PDF 文件。当我尝试打开文件时,它显示:文件读取错误。 文件类型不受支持或文件已损坏。

我的 logcat 没有任何错误。我正在使用 iTextg 5.5.3.jar,我使用 Android Studio 的 Gradle 文件按钮使用同步项目添加的。

我做错了什么?请帮助我。

这是 PDF 创建函数:

else if (id == R.id.PDFCreate)
        {
            Cursor cursor = db.rawQuery("SELECT * FROM '" + reciever +"' ",null);
            Cursor title = db.rawQuery("SELECT '" + reciever +"' FROM MasterClasslist", null);

            title.moveToFirst();
            //String filename = title.getString(title.getColumnIndex("Name"));
            String filename = "ClassManager.pdf";

            Document document = new Document();

            document.open();
            File root = new File(Environment.getExternalStorageDirectory(), "Class Manager Grades");

            if (!root.exists())
            {
                root.mkdirs();
            }

            File gpxfile = new File(root, filename);
            try
            {
                PdfWriter.getInstance(document,new FileOutputStream(gpxfile));
            }
            catch (DocumentException e)
            {
                e.printStackTrace();
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }

            Paragraph p3 = new Paragraph();
            p3.add("Class Manager");
            try
            {
                document.add(p3);
            }
            catch (DocumentException e)
            {
                e.printStackTrace();
            }

            PdfPTable table = new PdfPTable(6);

            table.addCell("First Name");
            table.addCell("Last Name");
            table.addCell("Prelim Grade");
            table.addCell("Midterm Grade");
            table.addCell("Final Grade");
            table.addCell("Semestral Grade");

            cursor.moveToFirst();
            int count = cursor.getCount();

            for (int j = 0; j < count; j++)
            {
                table.addCell(cursor.getString(cursor.getColumnIndex("FirstName")));
                table.addCell(cursor.getString(cursor.getColumnIndex("LastName")));
                table.addCell(cursor.getString(cursor.getColumnIndex("pGrade")));
                table.addCell(cursor.getString(cursor.getColumnIndex("mGrade")));
                table.addCell(cursor.getString(cursor.getColumnIndex("fGrade")));
                table.addCell(cursor.getString(cursor.getColumnIndex("semGrade")));

                cursor.moveToNext();
            }

            try
            {
                document.add(table);
            }
            catch (DocumentException e)
            {
                e.printStackTrace();
            }
            document.addCreationDate();

            Toast toast = Toast.makeText(getApplicationContext(), "PDF Created", Toast.LENGTH_SHORT);
            toast.show();


        }

此警告出现在 logcat:

02-25 14:10:04.903: W/System.err(11440): com.itextpdf.text.DocumentException: java.lang.NullPointerException
02-25 14:10:04.903: W/System.err(11440):    at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:809)
02-25 14:10:04.903: W/System.err(11440):    at com.itextpdf.text.Document.add(Document.java:278)
02-25 14:10:04.903: W/System.err(11440):    at test.com.classmanagertest.ClassViewStudents.onOptionsItemSelected(ClassViewStudents.java:628)
02-25 14:10:04.903: W/System.err(11440):    at android.app.Activity.onMenuItemSelected(Activity.java:2673)
02-25 14:10:04.903: W/System.err(11440):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1015)
02-25 14:10:04.903: W/System.err(11440):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-25 14:10:04.903: W/System.err(11440):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
02-25 14:10:04.903: W/System.err(11440):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-25 14:10:04.903: W/System.err(11440):    at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:177)
02-25 14:10:04.903: W/System.err(11440):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
02-25 14:10:04.903: W/System.err(11440):    at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
02-25 14:10:04.903: W/System.err(11440):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2949)
02-25 14:10:04.903: W/System.err(11440):    at android.widget.AbsListView.run(AbsListView.java:3683)
02-25 14:10:04.903: W/System.err(11440):    at android.os.Handler.handleCallback(Handler.java:733)
02-25 14:10:04.903: W/System.err(11440):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-25 14:10:04.903: W/System.err(11440):    at android.os.Looper.loop(Looper.java:149)
02-25 14:10:04.903: W/System.err(11440):    at android.app.ActivityThread.main(ActivityThread.java:5257)
02-25 14:10:04.903: W/System.err(11440):    at java.lang.reflect.Method.invokeNative(Native Method)
02-25 14:10:04.903: W/System.err(11440):    at java.lang.reflect.Method.invoke(Method.java:515)
02-25 14:10:04.903: W/System.err(11440):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-25 14:10:04.903: W/System.err(11440):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
02-25 14:10:04.903: W/System.err(11440):    at dalvik.system.NativeStart.main(Native Method)
02-25 14:10:04.903: W/System.err(11440): Caused by: java.lang.NullPointerException
02-25 14:10:04.903: W/System.err(11440):    at com.itextpdf.text.pdf.PdfDocument.flushLines(PdfDocument.java:1344)
02-25 14:10:04.903: W/System.err(11440):    at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:748)
02-25 14:10:04.913: W/System.err(11440):    ... 21 more

你的代码有两个问题:

  1. 您在附加 PdfWriter 之前打开 document。因此,PdfWriter 不会被告知 document 正在打开。

  2. 你最后没有关闭document。因此,PDF 输出仍未完成。

iText in Action 示例 HelloWorld 显示了正确的顺序:

    // step 1
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    document.add(new Paragraph("Hello World!"));
    // step 5
    document.close();