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

Discussion: HOW TO CREATE REPORT OF AN ITEXT PDF TABLE USING JAVA

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

Activities
Avatar
ATUAHENE OPPONG JONES:4/28/2017 11:22

I want to create a report of an Itext pdf table using java like what I have added to the file.please check and help me.

String ch = inv.getText();

JFileChooser chooser = new JFileChooser();
int dialogResult = chooser.showSa­veDialog(null);
//chooser.set­CurrentDirecto­ry(new java.io.File("."));
//chooser.set­DialogTitle("Cho­ose the destination path");
//
//chooser.set­FileSelection­Mode(JFileCho­oser.DIRECTORI­ES_ONLY); // Only folders
//chooser.setAc­ceptAllFileFil­terUsed(false);
//int okCancel = chooser.showSa­veDialog(null);
//String dataFolder = chooser.getCu­rrentDirectory() + ch +".pdf";;
if (dialogResult == JFileChooser.AP­PROVE_OPTION) {
String dataFolder=cho­oser.getSelec­tedFile().get­Path()+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(" ***Krez Technology ® ***\n 0504742788/02­45978356 \n"
+ ""
+ " KUMASI", FontFactory.get­Font(FontFacto­ry.HELVETICA_BOL­D, 14, 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(6);
header.setTable­Event(new DottedHeader());
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("AMT PAID");

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(6);
//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 KrezTechnology ©2017"));

// 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){
JOptionPane.show­MessageDialog(nu­ll, e.getMessage());
}
} else {
JOptionPane.show­ConfirmDialog(nu­ll,"cant save report"); // Show error message here or perform any failure action you want to
}

Reply
4/28/2017 11:22
Technology For Life
Avatar
Adam Ježek
Member
Avatar
Adam Ježek:4/29/2017 16:31

It's really hard to read the code you pasted. Try it again, but this time, wrap it with the code tags. It wil keep the formatting and use code highlighting. You can do this by clicking the </> symbol in the bar above the textbox. Then, paste the code between the tags like this:

[code]
//your code here
[/code]

 
Up Reply
4/29/2017 16:31
Avatar
ATUAHENE OPPONG JONES:5/2/2017 13:32
String ch = inv.getText();

JFileChooser chooser = new JFileChooser();
int dialogResult = chooser.showSaveDialog(null);
//chooser.setCurrentDirectory(new java.io.File("."));
//chooser.setDialogTitle("Choose the destination path");
//
//chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // Only folders
//chooser.setAcceptAllFileFilterUsed(false);
//int okCancel = chooser.showSaveDialog(null);
//String dataFolder = chooser.getCurrentDirectory() + ch +".pdf";;
if (dialogResult == JFileChooser.APPROVE_OPTION) {
String dataFolder=chooser.getSelectedFile().getPath()+ch+".pdf";;

try {

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

PdfWriter writer = PdfWriter.getInstance(my_pdf_report, new FileOutputStream(dataFolder));

my_pdf_report.open();
// // add image
// Image image=Image.getInstance(dataFolder+"ksoftlogo.jpg");
// my_pdf_report.add(new Paragraph("Ksoft",FontFactory.getFont(FontFactory.TIMES_BOLDITALIC,18,Font.BOLD,BaseColor.GREEN)));
// my_pdf_report.add(image);
PdfContentByte cb = writer.getDirectContent();
BarcodeEAN code = new BarcodeEAN();
//code.setCode("1234567891011");
Paragraph para = new Paragraph();

my_pdf_report.add(new Paragraph("Barcode UPC-A"));
code.setCodeType(Barcode.UPCA);
code.setCode("1098765432112");
my_pdf_report.add(code.createImageWithBarcode(cb, BaseColor.BLACK, BaseColor.BLACK));

my_pdf_report.add(para);

my_pdf_report.add(new Paragraph(" ***Krez Technology ® ***\n 0504742788/0245978356 \n"
+ ""
+ " KUMASI", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 14, Font.BOLD, BaseColor.BLUE)));
my_pdf_report.add(new Paragraph(" " + new Date().toGMTString()));
my_pdf_report.add(new Paragraph(" ========================================================================================================"));

// //creating title for my table

PdfPTable header = new PdfPTable(6);
header.setTableEvent(new DottedHeader());
header.addCell("INVOICE");
header.addCell("PRODUCT ID");
header.addCell("PRODUCT NAME");
header.addCell("QTY");
header.addCell("UNIT COST");
header.addCell("TOTAL QTY");

// header.addCell("AMT PAID");

my_pdf_report.add(header);

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

Class.forName(driver).newInstance();
con = DriverManager.getConnection(url + dbName, userName, password);

rs = stmt.executeQuery("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 */



//we have six columns in our table

PdfPTable my_report_table = new PdfPTable(6);
//create a cell object
PdfPCell table_cell;

while (rs.next()) {

String dept_id = rs.getString("Invoice");
table_cell=new PdfPCell(new Phrase(dept_id));
my_report_table.addCell(table_cell);

String pi=rs.getString("PID");
table_cell=new PdfPCell(new Phrase(pi));
my_report_table.addCell(table_cell);
String pn=rs.getString("Pname");
table_cell=new PdfPCell(new Phrase(pn));
my_report_table.addCell(table_cell);
String qt=rs.getString("Qtypurchased");
table_cell=new PdfPCell(new Phrase(qt));
my_report_table.addCell(table_cell);

String uc=rs.getString("Unitcost");
table_cell=new PdfPCell(new Phrase(uc));
my_report_table.addCell(table_cell);
String tc=rs.getString("TotalQty");
table_cell=new PdfPCell(new Phrase(tc));
my_report_table.addCell(table_cell);

String manager_id=rs.getString("Amtpay");
table_cell=new PdfPCell(new Phrase(manager_id));
my_report_table.addCell(table_cell);



}

/* Attach report table to PDF */
my_pdf_report.add(my_report_table);

/* Close all DB related objects */
//adding List Items
com.itextpdf.text.List list = new com.itextpdf.text.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 KrezTechnology ©2017"));

// my_pdf_report.close();
JOptionPane.showMessageDialog(null, "Report Saved\n"
+ "Please Check Report Folder"
);

my_pdf_report.close();
try {
File myFile = new File(dataFolder);
Desktop.getDesktop().open(myFile);

} catch (IOException ex) {
// no application registered for PDFs
JOptionPane.showMessageDialog(null, ex.getMessage());
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}

}
catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
} else {
JOptionPane.showConfirmDialog(null,"cant save report"); // Show error message here or perform any failure action you want to
}
Up Reply
5/2/2017 13:32
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
ATUAHENE OPPONG JONES:5/2/2017 13:33

i want it to display like the image attached.

Up Reply
5/2/2017 13:33
Technology For Life
Avatar
rudrapratap
Member
Avatar
rudrapratap:23. March 6:14

Your valuable contributions, unwavering commitment, and profound insights greatly enrich our learning journey. We truly appreciate your generous sharing of knowledge, as it fosters collective growth and enhances our educational endeavors. If you're seeking guidance on studying abroad, look no further! Study Abroad Our team of experienced consultants is available 24/7, standing by to assist you whenever you need support. Don't hesitate to reach out to us with any inquiries – we're here to help.

 
Up Reply
23. March 6:14
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

5 messages from 5 displayed.