1 package reports; 2 3 import com.lowagie.text.*; 4 import com.lowagie.text.pdf.*; 5 import java.io.ByteArrayOutputStream ; 6 import dinamica.*; 7 8 16 public class PDFEnhancedMasterDetail extends AbstractPDFOutput 17 { 18 19 PdfTemplate tpl = null; 20 BaseFont bf = null; 21 PdfContentByte cb = null; 22 23 26 protected void createPDF( 27 GenericTransaction data, 28 ByteArrayOutputStream buf) 29 throws Throwable 30 { 31 32 Document doc = new Document(); 34 PdfWriter docWriter = PdfWriter.getInstance(doc, buf); 35 doc.setPageSize(PageSize.LETTER); 36 37 HeaderFooter header = new HeaderFooter(new Phrase(getHeader()), false); 39 header.setBorder(Rectangle.BOTTOM); 40 header.setAlignment(Rectangle.ALIGN_CENTER); 41 doc.setHeader(header); 42 43 doc.open(); 44 45 docWriter.setPageEvent(new PageEvents()); 47 bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 48 cb = docWriter.getDirectContent(); 49 tpl = cb.createTemplate(20, 14); 50 51 docWriter.setViewerPreferences(PdfWriter.PageModeUseOutlines); 53 PdfOutline root = cb.getRootOutline(); 54 55 root = new PdfOutline(root, new PdfDestination(PdfDestination.XYZ, 0, docWriter.getVerticalPosition(false), 0), "Customer Data"); 57 Paragraph t = new Paragraph(getReportTitle(), new Font(Font.HELVETICA, 18f)); 58 t.setAlignment(Rectangle.ALIGN_CENTER); 59 doc.add(t); 60 61 PdfPTable custinfo = getCustomerSection(data); 63 doc.add(custinfo); 64 65 MasterDetailReader dataobj = (MasterDetailReader)data; 67 Recordset master = dataobj.getRecordset("master"); 68 master.top(); 69 while (master.next()) 70 { 71 doc.add(new Paragraph(" ")); 73 74 PdfPTable tbl = getGroupDetail(master, dataobj.getDetail(master)); 75 76 if (!docWriter.fitsPage(tbl, 25)) 77 doc.newPage(); 78 79 new PdfOutline(root, new PdfDestination(PdfDestination.XYZ, 0, docWriter.getVerticalPosition(false), 0), "Order #" + master.getString("orderid")); 81 doc.add( getGroupMaster(master) ); 82 83 doc.add( tbl ); 85 86 } 87 88 doc.add(new Paragraph(" ")); 89 90 new PdfOutline(root, new PdfDestination(PdfDestination.XYZ, 0, docWriter.getVerticalPosition(false), 0), "Grand Total"); 92 doc.add( getTotal(dataobj) ); 93 94 doc.close(); 95 docWriter.close(); 96 97 } 98 99 104 PdfPTable getCustomerSection(GenericTransaction data) throws Throwable 105 { 106 107 110 111 Recordset custinfo = data.getRecordset("query.sql"); 113 custinfo.first(); 114 115 PdfPTable datatable = new PdfPTable(2); 117 118 datatable.getDefaultCell().setPadding(3); 120 int headerwidths[] = {30, 70}; datatable.setWidths(headerwidths); 122 datatable.setWidthPercentage(50); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 124 datatable.getDefaultCell().setBorderWidth(1); 125 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); 126 127 PdfPCell c1 = new PdfPCell(new Phrase("Contacto")); 128 c1.setGrayFill(0.9f); 129 datatable.addCell(c1); 130 datatable.addCell(custinfo.getString("contactname")); 131 132 PdfPCell c2 = new PdfPCell(new Phrase("Empresa")); 133 c2.setGrayFill(0.9f); 134 datatable.addCell(c2); 135 datatable.addCell(custinfo.getString("companyname")); 136 137 PdfPCell c3 = new PdfPCell(new Phrase("Teléfono")); 138 c3.setGrayFill(0.9f); 139 datatable.addCell(c3); 140 datatable.addCell(custinfo.getString("phone")); 141 142 PdfPCell c4 = new PdfPCell(new Phrase("País")); 143 c4.setGrayFill(0.9f); 144 datatable.addCell(c4); 145 datatable.addCell(custinfo.getString("country")); 146 147 datatable.setSpacingBefore(10); 148 return datatable; 149 150 } 151 152 158 Paragraph getGroupMaster(Recordset master) throws Throwable 159 { 160 161 String text = "Order #" + master.getString("orderid"); 162 text = text + " Date: " + StringUtil.formatDate(master.getDate("orderdate"), "dd-MMM-yyyy"); 163 164 Paragraph t = new Paragraph(text,new Font(Font.HELVETICA, 12f, Font.BOLD)); 165 t.setAlignment(Rectangle.ALIGN_LEFT); 166 return t; 167 168 } 169 170 175 PdfPTable getGroupDetail(Recordset master, Recordset detail) throws Throwable 176 { 177 178 PdfPTable datatable = new PdfPTable(6); 180 181 datatable.getDefaultCell().setPadding(1); 183 int headerwidths[] = {35, 20, 10, 10, 10, 15}; datatable.setWidths(headerwidths); 185 datatable.setWidthPercentage(100); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 187 datatable.getDefaultCell().setBorderWidth(1); 188 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); 189 datatable.getDefaultCell().setGrayFill(0.9f); 190 datatable.addCell("Product"); 191 datatable.addCell("Category"); 192 datatable.addCell("Quantity"); 193 datatable.addCell("Unit Price"); 194 datatable.addCell("Discount"); 195 datatable.addCell("Sub-Total"); 196 197 datatable.setHeaderRows(1); 198 datatable.getDefaultCell().setGrayFill(0.0f); 199 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); 200 201 while (detail.next()) 203 { 204 String data = null; 205 206 datatable.addCell(detail.getString("productname")); 207 datatable.addCell(detail.getString("categoryname")); 208 209 PdfPCell c1 = new PdfPCell(new Phrase(detail.getString("quantity"))); 210 c1.setHorizontalAlignment(Element.ALIGN_RIGHT); 211 datatable.addCell(c1); 212 213 data = StringUtil.formatNumber(detail.getValue("unitprice"),"#,##0.00"); 214 PdfPCell c2 = new PdfPCell(new Phrase(data)); 215 c2.setHorizontalAlignment(Element.ALIGN_RIGHT); 216 datatable.addCell(c2); 217 218 data = StringUtil.formatNumber(detail.getValue("discount"),"#,##0.00"); 219 PdfPCell c3 = new PdfPCell(new Phrase(data)); 220 c3.setHorizontalAlignment(Element.ALIGN_RIGHT); 221 datatable.addCell(c3); 222 223 data = StringUtil.formatNumber(detail.getValue("subtotal"),"#,##0.00"); 224 PdfPCell c4 = new PdfPCell(new Phrase(data)); 225 c4.setHorizontalAlignment(Element.ALIGN_RIGHT); 226 datatable.addCell(c4); 227 228 } 229 230 PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD))); 232 c1.setColspan(5); 233 datatable.addCell(c1); 234 235 PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(master.getValue("total"), "#,##0.00"))); 236 c2.setHorizontalAlignment(Element.ALIGN_RIGHT); 237 datatable.addCell(c2); 238 239 datatable.setSpacingBefore(10); 240 return datatable; 241 242 } 243 244 249 PdfPTable getTotal(MasterDetailReader dataobj) throws Throwable 250 { 251 252 PdfPTable datatable = new PdfPTable(2); 254 255 datatable.getDefaultCell().setPadding(1); 257 int headerwidths[] = {85, 15}; datatable.setWidths(headerwidths); 259 datatable.setWidthPercentage(100); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 261 datatable.getDefaultCell().setBorderWidth(1); 262 datatable.getDefaultCell().setGrayFill(0.0f); 263 264 Recordset custinfo = dataobj.getRecordset("query.sql"); 266 custinfo.first(); 267 268 PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD))); 269 datatable.addCell(c1); 270 271 PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(custinfo.getValue("total"), "#,##0.00"))); 272 c2.setHorizontalAlignment(Element.ALIGN_RIGHT); 273 datatable.addCell(c2); 274 275 return datatable; 276 277 } 278 279 284 class PageEvents extends PdfPageEventHelper 285 { 286 287 public void onCloseDocument(PdfWriter writer, Document document) 288 { 289 int pageNum = writer.getPageNumber() - 1; 292 tpl.beginText(); 293 tpl.setFontAndSize(bf, 10); 294 tpl.showText(String.valueOf(pageNum)); 295 tpl.endText(); 296 297 } 298 299 public void onEndPage(PdfWriter writer, Document document) 300 { 301 302 String footer = writer.getPageNumber() + " of "; 306 float tWidth = bf.getWidthPoint(footer, 10); 307 float extraSpace = bf.getWidthPoint("00", 10); 308 float ty = document.bottom() - 14; float tx = document.right() - tWidth - extraSpace; cb.beginText(); 311 cb.setFontAndSize(bf, 10); 312 cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footer, tx, ty, 0); 313 cb.endText(); 314 cb.addTemplate(tpl, document.right() - extraSpace, ty); 315 316 } 317 318 } 319 320 } 321 | Popular Tags |