Get up to 80 % extra points for free! More info:

Discussion: Using Itext to create a report

In the previous quiz, Online Java Quiz, we tested our experience gained from the course.

Activities
Avatar
ATUAHENE OPPONG JONES:1/4/2017 5:43

please am using Itext to create report but everything goes to the App Data in the drive C:
please can you give me an alternative so that after installing the user can direct where he chooses to save the reports after generating. thanks.

String ch = inv.getText();
String dataFolder = System.getenv("AP­PDATA") + "/Stock reports/" + ch + ".pdf";

try {
Document my_pdf_report = new Document(new Rectangle(981.1f, 696.85f)) {
};

PdfWriter writer = PdfWriter.getIn­stance(my_pdf_re­port, new FileOutputStre­am(dataFolder));
my_pdf_report­.open();
// // add image
// Image image=Image.ge­tInstance(data­Folder+"
ksoftlo­go.jpg");
// my_pdf_report­.add(new Paragraph("Ksof­t",FontFactory­.getFont(FontFac­tory.TIMES_BOL­DITALIC,18,Fon­t.BOLD,BaseCo­lor.GREEN)));
// my_pdf_report­.add(image);
PdfContentByte cb = writer.getDirec­tContent();
BarcodeEAN code = new BarcodeEAN();
//code.setCode("12345678­91011");
Paragraph para = new Paragraph();

my_pdf_report­.add(new Paragraph("Barcode UPC-A"));
code.setCodeTy­pe(Barcode.UP­CA);
code.setCode("10987654­32112");
my_pdf_report­.add(code.cre­ateImageWithBar­code(cb, BaseColor.BLACK, BaseColor.BLACK));

my_pdf_report­.add(para);

my_pdf_report­.add(new Paragraph(" ***JUST LOVE ENTERPRISE ® ***\n 0241918131/02­41224726 \n"
+ ""
+ " KUMASI", FontFactory.get­Font(FontFacto­ry.HELVETICA_BOL­D, 24, Font.BOLD, BaseColor.BLUE)));
my_pdf_report­.add(new Paragraph(" " + new Date().toGMTStrin­g()));
my_pdf_report­.add(new Paragraph("=========­========================­========================­========================­======================="­));

// //creating title for my table

PdfPTable header = new PdfPTable(12);

header.addCell("IN­VOICE");
header.addCell("PRO­DUCT ID");
header.addCell("PRO­DUCT NAME");
header.addCell("QTY");
header.addCell("U­NIT COST");
header.addCell("TO­TAL QTY");
header.addCell("GRD TOTAL");
header.addCell("AMT PAID");
header.addCell("BAL");
header.addCell("Pay MODE");
header.addCell("STA­TUS");
header.addCell("BILL DATE");

my_pdf_report­.add(header);

String url = "jdbc:mysql://lo­calhost:3306/";
String dbName = "inventory";
String driver = "com.mysql.jdbc­.Driver";
String userName = "root";
String password = "";
try{

Class.forName(dri­ver).newInstan­ce();
con = DriverManager­.getConnection(url + dbName, userName, password);
// String sql = "select * from log";
// pst = con.prepareSta­tement(sql);
//
// rs = pst.executeQuery();
rs = stmt.executeQu­ery("SELECT p.Invoice, p.TotalQty, p.SubTotal, p.Amtpay, p.Bal, p.Paymode, p.Cstatus, p.BillDate, s.PID, s.Pname, s.Qtypurchased, s.Unitcost, s.TCost FROM productsales AS p Natural join sales AS s where Invoice like '%"+ch+"'");
/* Step-2: Initialize PDF my_pdf_reports - logical objects */

// PdfWriter.getIn­stance(my_pdf_re­port, new FileOutputStre­am("Report1.pdf"));
// my_pdf_report­.open();

//we have four columns in our table

PdfPTable my_report_table = new PdfPTable(12);
//create a cell object
PdfPCell table_cell;
System.out.prin­tln("check 2");
while (rs.next()) {

String dept_id = rs.getString("In­voice");
table_cell=new PdfPCell(new Phrase(dept_id));
my_report_table­.addCell(table_ce­ll);
String pi=rs.getStrin­g("PID");
table_cell=new PdfPCell(new Phrase(pi));
my_report_table­.addCell(table_ce­ll);
String pn=rs.getStrin­g("Pname");
table_cell=new PdfPCell(new Phrase(pn));
my_report_table­.addCell(table_ce­ll);
String qt=rs.getStrin­g("Qtypurchased");
table_cell=new PdfPCell(new Phrase(qt));
my_report_table­.addCell(table_ce­ll);

String uc=rs.getStrin­g("Unitcost");
table_cell=new PdfPCell(new Phrase(uc));
my_report_table­.addCell(table_ce­ll);
String tc=rs.getStrin­g("TotalQty");
table_cell=new PdfPCell(new Phrase(tc));
my_report_table­.addCell(table_ce­ll);

String surname=rs.get­String("SubTo­tal");
table_cell=new PdfPCell(new Phrase(surname));
my_report_table­.addCell(table_ce­ll);
String manager_id=rs­.getString("Am­tpay");
table_cell=new PdfPCell(new Phrase(manager_id));
my_report_table­.addCell(table_ce­ll);

String bal=rs.getStrin­g("Bal");
table_cell=new PdfPCell(new Phrase(bal));
my_report_table­.addCell(table_ce­ll);
String pmod=rs.getStrin­g("Paymode");
table_cell=new PdfPCell(new Phrase(pmod));
my_report_table­.addCell(table_ce­ll);
String cs=rs.getStrin­g("Cstatus");
table_cell=new PdfPCell(new Phrase(cs));
my_report_table­.addCell(table_ce­ll);
String bill=rs.getStrin­g("BillDate");
table_cell=new PdfPCell(new Phrase(bill));
my_report_table­.addCell(table_ce­ll);

}
/* Attach report table to PDF */
my_pdf_report­.add(my_repor­t_table);

/* Close all DB related objects */
//adding List Items
com.itextpdf.tex­t.List list = new com.itextpdf.tex­t.List(true, 20);
list.add("General Comment :............­.............­.............­..........\t \n...........­.............­.............­.............­.............­.............­....\n.......­.............­.............­.............­.............­.............­........\n");
list.add("Admin's Remarks :............­.............­.............­.............­.....\t \n...........­.............­.............­.............­.............­.............­....\n.......­.............­.............­.............­.............­.............­........\n");

my_pdf_report­.add(list);

//
my_pdf_report­.topMargin();
my_pdf_report­.add(new Paragraph(" \n "
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ " "
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ " Powered by T.K ©2016"));

// my_pdf_report­.close();
JOptionPane.show­MessageDialog(nu­ll, "Report Saved\n"
+ "Please Check Report Folder"
);
my_pdf_report­.close();

try {
File myFile = new File(dataFolder);
Desktop.getDes­ktop().open(my­File);
} catch (IOException ex) {
// no application registered for PDFs
JOptionPane.show­MessageDialog(nu­ll, ex.getMessage());
}
} catch (Exception e) {
JOptionPane.show­MessageDialog(nu­ll, e.getMessage());
}

}
catch(Exception e){

}

Reply
1/4/2017 5:43
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/4/2017 9:43

Please, use the </> button to insert source codes to the forum. The AppData folder is specified at the start of your code:

String dataFolder = System.getenv("APPDATA") + "/Stock reports/" + ch + ".pdf";

To make the user select a path in Java Swing, use JFileChooser instead:

JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Choose the destination path");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // Only folders
chooser.setAcceptAllFileFilterUsed(false);

if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
      String dataFolder = chooser.getCurrentDirectory() + ch + ".pdf";;
} else {
      // Show error message here or perform any failure action you want to
}

Also consider switching to JavaFX.

Edited 1/4/2017 9:43
Up Reply
1/4/2017 9:43
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
ATUAHENE OPPONG JONES:1/4/2017 11:07

am highly grateful it worked as i wanted. PERFECT.
Please Mr. David with respect to the JavaFX i have no idea but if you can give me tutorial notes .pdf on it i will do perfectly well.
hope to get that help from you because I Love working with java than any other platform.
thanks very much.
from Jones.

Up Reply
1/4/2017 11:07
Technology For Life
Avatar
ATUAHENE OPPONG JONES:1/4/2017 12:19

please the directory is ook but the pdf becomes empty without leaving the file (pdf) in it.
what should i do.

Up Reply
1/4/2017 12:19
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/4/2017 16:30

And are you sure that the code generating the PDF file works? If so, try to print what is in the dataFolder variable at the end:

System.out.println(dataFolder);

What does it print? Maybe I forgot to add a slash:

chooser.getCurrentDirectory() + "/" + ch + ".pdf";
Up Reply
1/4/2017 16:30
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
ATUAHENE OPPONG JONES:1/4/2017 16:54

the pdf file comes alright but cant save to the folder like my first code.
so please help me Boss

Up Reply
1/4/2017 16:54
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/5/2017 2:22

Try to print the path as I wrote in the last message and to add the extra slash.

Up Reply
1/5/2017 2:22
You can walk through a storm and feel the wind but you know you are not the wind.
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

7 messages from 7 displayed.