mardi 24 février 2015

Displaying a database in PDF using iText in Android Studio

I have a function that creates a PDF file that displays a database (SQLite). When I try to open the file, it says: File read error. File type is unsupportted or the file is corrupted.


I don't have any errors in my logcat. I am using iTextg 5.5.3.jar that I added using Sync Project with Gradle Files button of Android Studio.


What am I doing wrong? Please help me.


This is the PDF Create Function:





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();


}



Aucun commentaire:

Enregistrer un commentaire