Java tutorial
/* * Class : GenerateBillPDF.java * Developer : Laksh Lumba * Reviewer : * Description : * Created On : Dec 3, 2014, 2:25:52 PM * --------------------------------------------------------------------------- * Change History * Develper : * Reviwer : * Changed ON : * --------------------------------------------------------------------------- * 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 drugsupplychain.neu.css.gui.common.distributor; import com.itextpdf.text.Anchor; import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chapter; 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.Section; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import drugsupplychain.neu.css.model.address.Address; import drugsupplychain.neu.css.model.distributor.Distributor; import drugsupplychain.neu.css.model.order.Order; import drugsupplychain.neu.css.model.order.OrderItem; import drugsupplychain.neu.css.util.ImplCommonUtil; import java.io.FileOutputStream; /** * * @author Laksh */ public class GenerateBillPDF { private static String FILE = "C:/Users/Laksh/Desktop/Eclipse/NetbeansProject/DrugSupplyChain/Invoice.pdf"; private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 28, Font.BOLD); private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); public static void generateBill(Order order, Distributor distributor, Address billingAddress) { try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); //addMetaData(document); //addTitlePage(document); addContent(document, order, distributor, billingAddress); document.close(); } catch (Exception e) { e.printStackTrace(); } } /** * add content to the page * @param document * @throws DocumentException */ private static void addContent(Document document, Order order, Distributor distributor, Address billingAddress) throws DocumentException { Paragraph paragraph = new Paragraph(); Anchor anchor = new Anchor("DISTRIBUTOR INVOICE", catFont); anchor.setName("DISTRIBUTOR INVOICE"); // Second parameter is the number of the chapter Chapter catPart = new Chapter(new Paragraph(anchor), 1); Paragraph subPara = new Paragraph("CUSTOMER DETAILS", subFont); Section subCatPart = catPart.addSection(subPara); subCatPart.add(new Paragraph("ID: " + distributor.getOrganizationID())); subCatPart.add(new Paragraph("NAME: " + distributor.getName())); subCatPart.add(new Paragraph("LOCATION: " + distributor.getLocation())); subCatPart.add(new Paragraph("LICENSE NUMBER: " + distributor.getLincense().getLicenseNumber())); //add empty lines addEmptyLine(paragraph, 1); subCatPart.add(paragraph); subPara = new Paragraph("BILLING ADDRESS DETAILS", subFont); subCatPart = catPart.addSection(subPara); subCatPart.add(new Paragraph("ADDRESS LINE 1: " + billingAddress.getAddressLine1())); subCatPart.add(new Paragraph("ADDRESS LINE 2: " + billingAddress.getAddressLine2())); subCatPart.add(new Paragraph("CITY: " + billingAddress.getCity())); subCatPart.add(new Paragraph("STATE: " + billingAddress.getState())); subCatPart.add(new Paragraph("COUNTRY: " + billingAddress.getCountry())); subCatPart.add(new Paragraph("ZIP CODE: " + billingAddress.getZipcode())); //add empty lines addEmptyLine(paragraph, 1); subCatPart.add(paragraph); subPara = new Paragraph("ORDER SUMMARY TABLE", subFont); subCatPart = catPart.addSection(subPara); //add empty lines addEmptyLine(paragraph, 1); subCatPart.add(paragraph); // add a table int totalPrice = createTable(subCatPart, order); subCatPart.add(new Paragraph("Total Price: " + totalPrice)); subCatPart.add(new Paragraph( "ORDER DATE(MM/DD/YYYY): " + ImplCommonUtil.getFormattedDate(order.getCreationDate()))); subCatPart.add(new Paragraph("BILL GENERATED BY: " + System.getProperty("user.name"))); // now add all this to the document document.add(catPart); } /** * create table * @param subCatPart * @throws BadElementException */ private static int createTable(Section subCatPart, Order order) throws BadElementException { PdfPTable table = new PdfPTable(3); PdfPCell c1 = new PdfPCell(new Phrase("Order Item ID")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Quantity")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Value")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); int totalPrice = 0; for (OrderItem orderItem : order.getOrderItemList()) { table.addCell(orderItem.getProduct().getBarcode()); table.addCell(String.valueOf(orderItem.getQuantity())); table.addCell(String.valueOf(orderItem.getQuantity() * orderItem.getProduct().getPrice())); totalPrice = totalPrice + (orderItem.getQuantity() * orderItem.getProduct().getPrice()); } subCatPart.add(table); return totalPrice; } /** * add empty line to the pdf * @param paragraph * @param number */ private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } }