I am trying to generate a report in PDF format in which data is coming from Sqlite Database.I am using itextg library for generating PDF. But the problem is ,the generated pdf file shows only below content.
1.Expense Report
Report Generated on Tue Mar 07 17:50:43 GMT +05:30 2017
data which i am fetching from sqlite database is not getting shown Can somebody help me with issue which i am not able to identify.
Below is my Code:
public class GenerateReport extends Activity implements OnClickListener {
int date1, date2;
Cursor c;
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.BOLD);
private static String FILE;
Button bpdf;
int counter = 0;
static ArrayList<String> category;
static ArrayList<String> mode;
static ArrayList<Integer> amount;
static ArrayList<String> date;
View backg;
// You can access the static variables from nonstatic methods
private TextView text_date1, text_date2,tv_date1,tv_date2;
private DatePicker date_picker1, date_picker2;
private Button bstart_date, bend_date;
private int year1, year2;
private int month1, month2;
private int day1, day2;
static final int DATE_DIALOG_ID1 = 100;
static final int DATE_DIALOG_ID2 = 200;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.generate_report);
backg = findViewById(R.drawable.words_bgred);
loadPreference();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// sd card mounted
}
File direct = new File(Environment.getExternalStorageDirectory()
+ "/ExpenseManager");
if (!direct.exists()) {
if (direct.mkdir()) {
// directory is created;
}
}
bpdf = (Button) findViewById(R.id.bpdf_gen);
bpdf.setOnClickListener(this);
bpdf.setEnabled(false);
setCurrentDate();
addButtonListener1();
setEndDate();
addButtonListener2();
}
private static void addContent(Document document) throws DocumentException {
Anchor anchor = new Anchor("Expense Report", catFont);
anchor.setName("Expense Report");
Chapter catPart = new Chapter(new Paragraph(anchor), 1);
Paragraph paragraph = new Paragraph();
paragraph.add(new Paragraph("Report generated on " + new Date(),
smallBold));
addEmptyLine(paragraph, 3);
catPart.add(paragraph);
createTable(catPart);
document.add(catPart);
Log.e("meta", "content");
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
private void vibrate(int ms) {
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(ms);
}
private static void createTable(Section catPart) throws BadElementException {
PdfPTable table = new PdfPTable(4);
for (int i = 0; i < category.size(); i++) {
Log.e("category...", category.get(i));
Log.e("mode...", mode.get(i));
Log.e("amt...", "amt" + amount.get(i));
Log.e("date2...", "date" + date.get(i));
}
PdfPCell c1 = new PdfPCell(new Phrase("Category Name"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Amount Spent (Rs)"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Payment Mode"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
for (int i = 0; i < category.size(); i++) {
table.addCell(category.get(i));
table.addCell(amount.get(i) + "");
table.addCell(date.get(i));
table.addCell(mode.get(i));
}
catPart.add(table);
Log.e("meta", "table");
}
private static void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
@Override
public void onClick(View arg0) {
// vibrate(50);
// TODO Auto-generated method stub
try {
category = new ArrayList<String>();
mode = new ArrayList<String>();
amount = new ArrayList<Integer>();
date = new ArrayList<String>();
if (date1 > date2) {
Toast.makeText(getApplicationContext(),
"Start Date should be less than End Date",
Toast.LENGTH_LONG).show();
Log.e("date1", date1 + " " + date2);
}
else {
DbClass dc = new DbClass(this);
dc.open();
c = dc.showResultInPdf(date1, date2);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
category.add(c.getString(1));
mode.add(c.getString(4));
amount.add(c.getInt(2));
String change = c.getString(3);
change = change.substring(0, 8);
Log.e("change", change);
date.add(change);
}
dc.close();
Toast.makeText(
getApplicationContext(),
"Pdf Report Generated and saved in \n "
+ Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/ExpenseManager",
Toast.LENGTH_LONG).show();
final Calendar calendar = Calendar.getInstance();
int year_current = calendar.get(Calendar.YEAR);
int month_current = calendar.get(Calendar.MONTH) + 1;
int day_current = calendar.get(Calendar.DAY_OF_MONTH);
String print = day_current + "-" + month_current + "-"
+ year_current;
savePreference();
FILE = Environment.getExternalStorageDirectory()
+ "/ExpenseManager/ExpenseManager" + " " + print + "("
+ counter + ")" + ".pdf";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addContent(document);
document.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private void savePreference() {
counter++;
SharedPreferences sp = getSharedPreferences("AppSharedPref", 0);
Editor editor = sp.edit();
editor.putInt("count", counter);
editor.commit();
}
private void loadPreference() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 0);
counter = sp.getInt("count", 0);
}
public void setCurrentDate() {
text_date1 = (TextView) findViewById(R.id.text_date1);
tv_date1 = (TextView) findViewById(R.id.date_label1);
date_picker1 = (DatePicker) findViewById(R.id.date_picker1);
final Calendar calendar = Calendar.getInstance();
year1 = calendar.get(Calendar.YEAR);
month1 = calendar.get(Calendar.MONTH);
day1 = calendar.get(Calendar.DAY_OF_MONTH);
text_date1.setText(new StringBuilder()
.append(day1).append("/").append(month1 + 1).append("/")
.append(year1).append(" "));
date_picker1.init(year1, month1, day1, null);
String s = new StringBuilder().append(year1).append(month1 + 1)
.append(day1).toString();
date1 = Integer.parseInt(s);
}
public void setEndDate() {
text_date2 = (TextView) findViewById(R.id.text_date2);
tv_date2 = (TextView) findViewById(R.id.date_label2);
date_picker2 = (DatePicker) findViewById(R.id.date_picker2);
final Calendar calendar = Calendar.getInstance();
year2 = calendar.get(Calendar.YEAR);
month2 = calendar.get(Calendar.MONTH);
day2 = calendar.get(Calendar.DAY_OF_MONTH);
// set current date into textview
text_date2.setText(new StringBuilder()
// Month is 0 based, so you have to add background1
.append(day2).append("/").append(month2 + 1).append("/")
.append(year2).append(" "));
// set current date into Date Picker
date_picker2.init(year2, month2, day2, null);
String s = new StringBuilder().append(year2).append(month2 + 1)
.append(day2).toString();
date2 = Integer.parseInt(s);
}
public void addButtonListener1() {
bstart_date = (Button) findViewById(R.id.bstart_date);
bstart_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID1);
}
});
}
public void addButtonListener2() {
bend_date = (Button) findViewById(R.id.bend_date);
bend_date.setEnabled(false);
bend_date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID2);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == DATE_DIALOG_ID1) {
return new DatePickerDialog(this, datePickerListener1, year1,
month1, day1);
}
else if (id == DATE_DIALOG_ID2) {
return new DatePickerDialog(this, datePickerListener2, year2,
month2, day2);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener1 = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year1 = selectedYear;
month1 = selectedMonth + 1;
day1 = selectedDay;
// set selected date into Text View
text_date1.setText(new StringBuilder().append(day1).append("/")
.append(month1).append("/").append(year1).append(" "));
text_date1.setVisibility(View.VISIBLE);
tv_date1.setVisibility(View.VISIBLE);
// set selected date into Date Picker
date_picker1.init(year1, month1, day1, null);
String s = year1 + "" + (month1 < 10 ? ("0" + month1) : (month1))
+ "" + (day1 < 10 ? ("0" + day1) : (day1));
date1 = Integer.parseInt(s);
bend_date.setEnabled(true);
}
};
private DatePickerDialog.OnDateSetListener datePickerListener2 = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year2 = selectedYear;
month2 = selectedMonth + 1;
day2 = selectedDay;
text_date2.setText(new StringBuilder().append(day2).append("/")
.append(month2).append("/").append(year2).append(" "));
text_date2.setVisibility(View.VISIBLE);
tv_date2.setVisibility(View.VISIBLE);
date_picker2.init(year2, month2, day2, null);
String s = year2 + "" + (month2 < 10 ? ("0" + month2) : (month2))
+ "" + (day2 < 10 ? ("0" + day2) : (day2));
date2 = Integer.parseInt(s);
bpdf.setEnabled(true);
}
};
the tables were not getting created properly thats why data was not getting saved into the pdf.