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.
Member
5 messages from 5 displayed.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
In the previous quiz, Online Java Quiz, we tested our experience gained from the course.
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]
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
}
i want it to display like the image attached.
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.
5 messages from 5 displayed.