package service;
import hibernateSwingApi.component.CTable;
import hibernateSwingApi.utils.Utils;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.toedter.calendar.JDateChooser;
public class JTableToPDF {
private JTable table;
private String fileDir;
public JTableToPDF(CTable table1, JDateChooser ilkTarih,
JDateChooser sonTarih, JLabel genelToplam, JLabel iskontoTutari,
JLabel kdvTutari) {
this.table = table1;
fileDir = System.getProperty("user.home") + "/PSAMerkez+Dosyalar/SatisRapor.pdf";
Object data[][] = new String[table.getRowCount()][table
.getColumnCount()];
String col[] = new String[table.getColumnCount()];
for (int i = 0; i < table.getColumnCount(); i++) {
col[i] = table.getColumnName(i);
}
for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
Class<?> columnClass = table.getColumnClass(j);
if (columnClass.equals(String.class)) {
data[i][j] = table.getValueAt(i, j);
} else if (columnClass.equals(Double.class)) {
double b = (double) table.getValueAt(i, j);
String a = new Double(b).toString();
data[i][j] = a;
} else {
Date date = (Date) table.getValueAt(i, j);
String tarih = date.toString();
data[i][j] = tarih;
}
}
}
DefaultTableModel model = new DefaultTableModel(data, col);
table = new JTable(model);
try {
int count = table.getRowCount();
Document document = new Document();
File file = new File(fileDir);
if (file.exists()) {
if (file.delete()) {
PdfWriter.getInstance(document, new FileOutputStream(
fileDir));
} else {
Utils.showErrorMessage("Lütfen dosyayı kapatıp tekrar deneyiniz !");
return;
}
} else {
PdfWriter.getInstance(document, new FileOutputStream(fileDir));
}
String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date())
.toString();
String headerString = "Rapor Tarihi: " + date + " "
+ "ALTISOFT /" + " "
+ "www.altisoft.com.tr / info@altisoft.com.tr";
HeaderFooter hf = new HeaderFooter(new Phrase(headerString), false);
document.setFooter(hf);
document.open();
BaseFont bf = BaseFont.createFont("C:\\WINDOWS\\Fonts\\Arial.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f1 = new Font(bf, 10);
Paragraph paragraph = new Paragraph("SATIŞ RAPORU", f1);
paragraph.setFont(new Font(14));
paragraph.setSpacingAfter(20);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
Calendar calIlk = Calendar.getInstance();
calIlk.setTime(ilkTarih.getDate());
int ilkDay = calIlk.get(Calendar.DAY_OF_MONTH);
int ilkMounth = calIlk.get(Calendar.MONTH) + 1;
int ilkYear = calIlk.get(Calendar.YEAR);
calIlk.setTime(sonTarih.getDate());
int sonDay = calIlk.get(Calendar.DAY_OF_MONTH);
int sonMounth = calIlk.get(Calendar.MONTH) + 1;
int sonYear = calIlk.get(Calendar.YEAR);
Paragraph tarih = new Paragraph("Baslangıç Tarihi" + " " + ilkDay
+ "/" + ilkMounth + "/" + ilkYear + " Bitiş Tarihi "
+ sonDay + "/" + sonMounth + "/" + sonYear, f1);
tarih.setFont(new Font(8));
tarih.setSpacingAfter(20);
tarih.setAlignment(Element.ALIGN_LEFT);
document.add(tarih);
PdfPTable tab = new PdfPTable(table.getColumnCount());
tab.setWidthPercentage(100);
for (int i = 0; i < table.getColumnCount(); i++) {
PdfPCell cell = new PdfPCell(new Paragraph(
table.getColumnName(i), f1));
tab.addCell(cell);
}
Font f2 = new Font(bf, 8);
for (int i = 0; i < count; i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
Object obj1 = GetData(table, i, j);
String value1 = obj1.toString();
PdfPCell cell = new PdfPCell(new Paragraph(value1, f2));
tab.addCell(cell);
}
}
document.add(tab);
Paragraph kdvTutarParag = new Paragraph("Toplam KDV Tutarı : "
+ kdvTutari.getText() + " TL");
kdvTutarParag.setFont(new Font(10));
kdvTutarParag.setSpacingBefore(20);
kdvTutarParag.setAlignment(Element.ALIGN_RIGHT);
document.add(kdvTutarParag);
Paragraph iskontoTutarParag = new Paragraph(
"Toplam İskonto Tutarı : " + iskontoTutari.getText()
+ " TL");
iskontoTutarParag.setFont(new Font(10));
iskontoTutarParag.setSpacingBefore(0);
iskontoTutarParag.setAlignment(Element.ALIGN_RIGHT);
document.add(iskontoTutarParag);
Paragraph genelToplamParag = new Paragraph("Genel Toplam : "
+ genelToplam.getText() + " TL");
genelToplamParag.setFont(new Font(10));
genelToplamParag.setSpacingBefore(0);
genelToplamParag.setAlignment(Element.ALIGN_RIGHT);
document.add(genelToplamParag);
document.close();
File pdfFile = new File(fileDir);
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
Utils.showErrorMessage("Dosya açılamıyor !");
}
} else {
Utils.showErrorMessage("Dosya bulunamadı !");
}
} catch (Exception e) {
}
}
public Object GetData(JTable table, int row_index, int col_index) {
return table.getModel().getValueAt(row_index, col_index);
}
}