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 Utils; import bean.Customer; import bean.CustomerService; import bean.Room; import bean.RoomStatus; import com.itextpdf.io.font.FontConstants; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfName; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import com.itextpdf.layout.Document; import com.itextpdf.layout.Style; import com.itextpdf.layout.border.Border; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.ListItem; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.element.Text; import com.itextpdf.layout.property.HorizontalAlignment; import com.itextpdf.layout.property.TextAlignment; import com.itextpdf.layout.renderer.CellRenderer; import com.itextpdf.layout.renderer.DrawContext; import common.CommonFunction; import common.Constant; import java.io.File; import java.io.IOException; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import library.LibraryString; import model.ModelCustomerService; import model.ModelRoom; import model.ModelRoomStatus; /** * * @author GloomySunday */ public class PdfUtil { private final Customer mCustomer; private final RoomStatus mRoomStatus; private final List<CustomerService> mListCustomerService; private static final String BASE_DESTINATION = "bill"; private final SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyyMMdd"); private PdfFont mCommonFont; public PdfUtil(Customer mCustomer) { this.mCustomer = mCustomer; mRoomStatus = (new ModelRoomStatus()).getItemByIdCustomer(mCustomer.getIdCustomer()); mListCustomerService = (new ModelCustomerService()).getListByIdCustomer(mCustomer.getIdCustomer()); } private String createDestination() { String destination = BASE_DESTINATION + File.separator + mSimpleDateFormat.format(new Date(System.currentTimeMillis())) + File.separator + LibraryString.createSlug(mCustomer.getFullname()) + "_" + mCustomer.getIdCustomer() + ".pdf"; File file = new File(destination); file.getParentFile().mkdirs(); return destination; } public void writeDocument() { try { PdfWriter writer = new PdfWriter(createDestination()); PdfDocument pdf = new PdfDocument(writer); Document document = new Document(pdf); mCommonFont = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); addTitle(document); addContent(document); addFooter(document); document.close(); } catch (Exception ex) { Logger.getLogger(PdfUtil.class.getName()).log(Level.SEVERE, null, ex); } } private void addTitle(Document document) throws IOException { Style styleTitle = new Style(); styleTitle.setFont(mCommonFont).setFontSize(20).setTextAlignment(TextAlignment.CENTER) .setHorizontalAlignment(HorizontalAlignment.CENTER); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); document.add(new Paragraph("Ha ?n Thanh Ton - Ngy: " + sdf.format(System.currentTimeMillis())) .addStyle(styleTitle)); } private void addContent(Document document) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Paragraph paragraphCustomer = new Paragraph(); paragraphCustomer.add(new Text("Khch hng ").setFont(mCommonFont)); com.itextpdf.layout.element.List listCustomer = new com.itextpdf.layout.element.List().setSymbolIndent(12) .setListSymbol("\u2022").setFont(mCommonFont); listCustomer.add(new ListItem("H? tn: " + mCustomer.getFullname())) .add(new ListItem("Chng minh nhn dn: " + mCustomer.getIdentityCode())) .add(new ListItem("S in thoi: " + mCustomer.getPhoneNumber())); listCustomer.setMarginLeft(30).setFont(mCommonFont); Room objRoom = (new ModelRoom()).getItemById(mRoomStatus.getIdRoom()); Paragraph paragraphRoom = new Paragraph(); paragraphRoom.add(new Text("Phng").setFont(mCommonFont)); com.itextpdf.layout.element.List listRoom = new com.itextpdf.layout.element.List().setSymbolIndent(12) .setListSymbol("\u2022").setFont(mCommonFont); listRoom.add(new ListItem("Tn phng: " + objRoom.getRoomName())) .add(new ListItem("Loi phng: " + Constant.ROOM_TYPE_NAME[objRoom.getType()])) .add(new ListItem("Gi: " + CommonFunction.convertDoubleToMoney(objRoom.getPrice()) + "")) .add(new ListItem("Ngy n: " + sdf.format(mRoomStatus.getDateCome()))) .add(new ListItem("Ngy i: " + sdf.format(mRoomStatus.getDateLeave()))); listRoom.setMarginLeft(30).setFont(mCommonFont); Paragraph paragraphService = new Paragraph(); paragraphService.add(new Text("Dch v").setFont(mCommonFont)); Table table = new Table(new float[] { 10f, 7f, 5f, 5f, 7f }); table.setWidthPercent(100).setTextAlignment(TextAlignment.CENTER) .setHorizontalAlignment(HorizontalAlignment.CENTER); String[] header = { "Tn dch v", "Gi ti?n", "S lng", "Ngy s dng" }; for (int i = 0, k = header.length; i < k; i++) { Cell cellHeader = new Cell(); cellHeader.add(header[i]); cellHeader.setNextRenderer(new RoundedCornersCellRenderer(cellHeader)); cellHeader.setPadding(5).setBorder(Border.NO_BORDER); table.addHeaderCell(cellHeader); } double total = 0; for (CustomerService objCustomerService : mListCustomerService) { total += (objCustomerService.getPrice() * objCustomerService.getNumber()); for (int i = 0; i < 4; i++) { Cell cell = new Cell(); switch (i) { case 0: cell.add(objCustomerService.getServiceName()); break; case 1: cell.add(CommonFunction.convertDoubleToMoney(objCustomerService.getPrice()) + ""); break; case 2: cell.add(objCustomerService.getNumber() + ""); break; case 3: cell.add(sdf.format(objCustomerService.getDateUsed())); break; } table.addCell(cell); } } Cell cellFooter = new Cell(1, 3); cellFooter.add("Tng ti?n: "); table.addCell(cellFooter); cellFooter = new Cell(); cellFooter.add(CommonFunction.convertDoubleToMoney(total) + ""); table.addCell(cellFooter); document.add(paragraphCustomer).add(listCustomer); document.add(paragraphRoom).add(listRoom); document.add(paragraphService).add(table); } private void addFooter(Document document) { Paragraph paragraph = new Paragraph("Khch hng ").setTextAlignment(TextAlignment.LEFT) .setUnderline().add("Nhn vin").setTextAlignment(TextAlignment.RIGHT).setUnderline() .setFont(mCommonFont); document.add(paragraph); paragraph.addStyle(new Style().setFont(mCommonFont).setTextAlignment(TextAlignment.LEFT) .setHorizontalAlignment(HorizontalAlignment.LEFT).setUnderline()); document.add(paragraph); } private class RoundedCornersCellRenderer extends CellRenderer { public RoundedCornersCellRenderer(Cell modelElement) { super(modelElement); } @Override public void drawBorder(DrawContext drawContext) { Rectangle rectangle = getOccupiedAreaBBox(); float llx = rectangle.getX() + 1; float lly = rectangle.getY() + 1; float urx = rectangle.getX() + getOccupiedAreaBBox().getWidth() - 1; float ury = rectangle.getY() + getOccupiedAreaBBox().getHeight() - 1; PdfCanvas canvas = drawContext.getCanvas(); float r = 4; float b = 0.4477f; canvas.moveTo(llx, lly).lineTo(urx, lly).lineTo(urx, ury - r) .curveTo(urx, ury - r * b, urx - r * b, ury, urx - r, ury).lineTo(llx + r, ury) .curveTo(llx + r * b, ury, llx, ury - r * b, llx, ury - r).lineTo(llx, lly).stroke(); super.drawBorder(drawContext); } } }