Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.grant.report; import com.grant.data.ItemDAO; import com.grant.entity.PrintDetails; import java.io.FileOutputStream; import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.util.Vector; /** * * @author Isura Amarasinghe */ public class BillPdf { private String FILE = "E:/FirstPdf6.pdf"; private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); private Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); private Font smallBold2 = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.ITALIC); private Font smallBold3 = new Font(Font.FontFamily.TIMES_ROMAN, 9, Font.NORMAL); private Font verySmBold = new Font(Font.FontFamily.TIMES_ROMAN, 7, Font.BOLD); String invoNo = null; public void printBill(PrintDetails printDetails) { try { String newInvo = printDetails.getInvoiceNo().replaceAll("\\|", "_"); FILE = "E:/" + newInvo + ".pdf"; invoNo = printDetails.getInvoiceNo(); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); addMetaData(document); addTitlePage(document, printDetails); //addContent(document); document.close(); } catch (FileNotFoundException | DocumentException e) { e.printStackTrace(); } } // iText allows to add metadata to the PDF which can be viewed in your Adobe // Reader // under File -> Properties private void addMetaData(Document document) { document.addTitle("My first PDF"); document.addSubject("Using iText"); document.addKeywords("Java, PDF, iText"); document.addAuthor("Lars Vogel"); document.addCreator("Lars Vogel"); } private void addTitlePage(Document document, PrintDetails printDetails) throws DocumentException { Paragraph preface = new Paragraph(); // We add one empty line addEmptyLine(preface, 1); // Lets write a big header preface.add(new Paragraph("GRANT TECH HOLDINGS", catFont)); addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date preface.add(new Paragraph("No, 407 A, Colombo Road, Pepiliyana", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ subFont)); preface.add(new Paragraph("Tel/Fax :0112 199 100 | 0719 192 815 | 0719 392 815", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ subFont)); preface.add(new Paragraph("E-mail :granttech@sltnet.lk | granttechholdings@gmail.com", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ subFont)); preface.add(new Paragraph( "____________________________________________________________________________________", smallBold)); preface.add(new Paragraph("Customer Name : " + printDetails.getCustomerName(), smallBold)); preface.add(new Paragraph("Address : " + printDetails.getAddress(), smallBold)); preface.add(new Paragraph( "Date: " + printDetails.getInvoiceNo() + " | Order No: " + printDetails.getOrderNo(), smallBold)); preface.add(new Paragraph("Pay Type: " + printDetails.getPayType() + " | Sales Code: " + printDetails.getCustomerName(), smallBold)); preface.add(new Paragraph("Cheque No: " + printDetails.getNoPay(), smallBold)); createTable(preface); Paragraph preface2 = new Paragraph(); preface2.setIndentationLeft(350); preface2.add(new Paragraph("Sub Total : " + printDetails.getSubTotal(), smallBold)); preface2.add(new Paragraph("Discount : " + printDetails.getDiscount() + " %", smallBold)); preface2.add(new Paragraph("Total : " + printDetails.getTotal(), smallBold)); Paragraph preface3 = new Paragraph(); preface3.add(new Paragraph( "____________________________________________________________________________________", smallBold)); preface3.add(new Paragraph("Goods once sold will not be taken back. ", smallBold2)); preface3.add(new Paragraph( "Cheques to be drawn in favour of Grant Tech Holdings (Pvt) Ltd 1146009208-Commercial Bank-Wadduwa. ", verySmBold)); preface3.add(new Paragraph("goods are accepted in good condition. ", verySmBold)); preface3.add(new Paragraph( "____________________________________________________________________________________", smallBold)); preface3.add(new Paragraph( "Name:___________________________________________ Signature:__________________________________________", smallBold3)); preface3.add(new Paragraph( "I.D No (If credit):__________________________________ Date:______________________________________________", smallBold3)); preface3.add(new Paragraph( "Checked by:_______________________________________ Authorized by:_______________________________________", smallBold3)); preface3.add(new Paragraph( "____________________________________________________________________________________", smallBold)); // now add all this to the document document.add(preface); document.add(preface2); document.add(preface3); document.newPage(); } private void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } private void createTable(Paragraph preface) throws BadElementException, DocumentException { addEmptyLine(preface, 1); PdfPTable table = new PdfPTable(6); // t.setBorderColor(BaseColor.GRAY); // t.setPadding(4); // t.setSpacing(4); // t.setBorderWidth(1); PdfPCell c1 = new PdfPCell(new Phrase("Item Code")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Description")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Qty")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Unit")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Rate")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Amount")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); float[] columnWidths = new float[] { 15f, 30f, 10f, 10f, 10f, 15f }; table.setWidths(columnWidths); ItemDAO dao = new ItemDAO(); String itemName; String itemNo; String description; String outwards; String unitPrice; String amount; String unit; Vector<Vector<String>> data; data = dao.getStockOutTbl(invoNo); String s = data.get(0).get(1); for (int i = 0; i < data.size(); i++) { Vector<String> data2 = data.get(i); itemName = data.get(i).get(0); description = data.get(i).get(3); outwards = data.get(i).get(5); itemNo = data.get(i).get(1); unit = data.get(i).get(6); unitPrice = data.get(i).get(8); amount = data.get(i).get(9); table.addCell(itemNo); table.addCell(description); table.addCell(outwards); table.addCell(unit); table.addCell(unitPrice); table.addCell(amount); } preface.add(table); } }