List of usage examples for com.lowagie.text Rectangle NO_BORDER
int NO_BORDER
To view the source code for com.lowagie.text Rectangle NO_BORDER.
Click Source Link
From source file:biblivre3.cataloging.bibliographic.BiblioBO.java
License:Open Source License
public MemoryFileDTO createFileLabelsPDF(ArrayList<LabelDTO> labels, LabelConfigDTO labelConfig) { Document document = new Document(); final MemoryFileDTO file = new MemoryFileDTO(); file.setFileName("biblivre_etiquetas_" + new Date().getTime() + ".pdf"); try {//from w w w . ja v a 2s.c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.setPageSize(PageSize.A4); float verticalMargin = (297.0f - (labelConfig.getHeight() * labelConfig.getRows())) / 2; document.setMargins(7.15f * ApplicationConstants.MM_UNIT, 7.15f * ApplicationConstants.MM_UNIT, verticalMargin * ApplicationConstants.MM_UNIT, verticalMargin * ApplicationConstants.MM_UNIT); document.open(); PdfPTable table = new PdfPTable(labelConfig.getColumns()); table.setWidthPercentage(100f); PdfPCell cell; int i = 0; for (i = 0; i < labelConfig.getOffset(); i++) { cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); cell.setFixedHeight(labelConfig.getHeight() * ApplicationConstants.MM_UNIT); table.addCell(cell); } for (LabelDTO ldto : labels) { PdfContentByte cb = writer.getDirectContent(); String holdingSerial = String.valueOf(ldto.getHoldingSerial()); while (holdingSerial.length() < 10) { holdingSerial = "0" + holdingSerial; } Barcode39 code39 = new Barcode39(); code39.setExtended(true); code39.setCode(holdingSerial); code39.setStartStopText(false); Image image39 = code39.createImageWithBarcode(cb, null, null); if (labelConfig.getHeight() > 30.0f) { image39.scalePercent(110f); } else { image39.scalePercent(90f); } Paragraph para = new Paragraph(); Phrase p1 = new Phrase(StringUtils.left(ldto.getAuthor(), 28) + "\n"); Phrase p2 = new Phrase(StringUtils.left(ldto.getTitle(), 28) + "\n\n"); Phrase p3 = new Phrase(new Chunk(image39, 0, 0)); para.add(p1); para.add(p2); para.add(p3); cell = new PdfPCell(para); i++; cell.setNoWrap(true); cell.setFixedHeight(labelConfig.getHeight() * ApplicationConstants.MM_UNIT); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); Paragraph para2 = new Paragraph(); Phrase p5 = new Phrase(ldto.getLocationA() + "\n"); Phrase p6 = new Phrase(ldto.getLocationB() + "\n"); Phrase p7 = new Phrase(ldto.getLocationC() + "\n"); Phrase p8 = new Phrase(ldto.getLocationD() + "\n"); Phrase p4 = new Phrase(ldto.getAssetHolding() + "\n"); para2.add(p5); para2.add(p6); para2.add(p7); para2.add(p8); para2.add(p4); cell = new PdfPCell(para2); i++; cell.setFixedHeight(labelConfig.getHeight() * ApplicationConstants.MM_UNIT); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); } if ((i % labelConfig.getColumns()) != 0) { while ((i % labelConfig.getColumns()) != 0) { i++; cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); } } document.add(table); writer.flush(); document.close(); file.setFileData(baos.toByteArray()); } catch (Exception de) { System.out.println(de.getMessage()); } return file; }
From source file:biblivre3.circulation.CirculationBO.java
License:Open Source License
public MemoryFileDTO createFileUserCardsPDF(ArrayList<UserCardDTO> cards, int startOffset, Properties i18n) { Document document = new Document(); MemoryFileDTO file = new MemoryFileDTO(); file.setFileName("user_cards_" + new Date().getTime() + ".pdf"); try {// w ww . ja v a 2 s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.setPageSize(PageSize.A4); document.setMargins(7.15f * ApplicationConstants.MM_UNIT, 7.15f * ApplicationConstants.MM_UNIT, 9.09f * ApplicationConstants.MM_UNIT, 9.09f * ApplicationConstants.MM_UNIT); document.open(); PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100f); PdfPCell cell; int i = 0; for (i = 0; i < startOffset; i++) { cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); cell.setFixedHeight(46.47f * ApplicationConstants.MM_UNIT); table.addCell(cell); } for (UserCardDTO card : cards) { String userEnrollNumber = String.valueOf(card.getUserId()); PdfContentByte cb = writer.getDirectContent(); Barcode39 code39 = new Barcode39(); code39.setExtended(true); while (userEnrollNumber.length() < 10) { userEnrollNumber = "0" + userEnrollNumber; } code39.setCode(userEnrollNumber); code39.setStartStopText(false); Image image39 = code39.createImageWithBarcode(cb, null, null); image39.scalePercent(110f); Paragraph para = new Paragraph(); String name = card.getUserName(); name = name.length() >= 30 ? name.substring(0, 30) : name; Phrase p1 = new Phrase(name + "\n"); Phrase p2 = new Phrase(new Chunk(image39, 0, 0)); Phrase p3 = new Phrase( I18nUtils.getText(i18n, "LABEL_USER_SERIAL") + ": " + card.getUserId() + "\n"); Phrase p4 = new Phrase( I18nUtils.getText(i18n, "LABEL_USER_TYPE") + ": " + card.getUserType() + "\n\n"); para.add(p1); para.add(p3); para.add(p4); para.add(p2); cell = new PdfPCell(para); i++; cell.setFixedHeight(46.47f * ApplicationConstants.MM_UNIT); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); } if ((i % 3) != 0) { while ((i % 3) != 0) { i++; cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); } } document.add(table); writer.flush(); document.close(); writer.close(); file.setFileData(baos.toByteArray()); } catch (DocumentException de) { System.out.println(de.getMessage()); } return file; }
From source file:bucks.AuditSheet.java
License:Open Source License
void generateSheet(HttpServletResponse res, List<Batch> batches) { ///*from w w w. ja v a 2s . com*/ // paper size legal (A4) 8.5 x 11 // page 1-inch = 72 points // String spacer = " \n"; ServletOutputStream out = null; String filename = "marketbucks_audit_sheet.pdf"; try { // space // Rectangle pageSize = new Rectangle(612, 792); // 8.5" X 11" Document document = new Document(pageSize, 36, 36, 18, 18);// 18,18,54,35 ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); URL imgUrl = new URL(imageUrl); Image image = Image.getInstance(imgUrl); image.scalePercent(20f); // image.scaleAbsolute(100f, 100f);// width, height // image.setRotation(3.1f); // in radians // image.setRotationDegrees(90f); // degrees // image.setSpacingBefore(10f); // image.setSpacingAfter(10f); // image.setWidthPercentage(25.0f); Font fnt = new Font(Font.TIMES_ROMAN, 12, Font.NORMAL); Font fntb = new Font(Font.TIMES_ROMAN, 12, Font.BOLD); Font fntb2 = new Font(Font.TIMES_ROMAN, 14, Font.BOLD); Phrase spacePhrase = new Phrase(); Chunk ch = new Chunk(spacer, fnt); spacePhrase.add(ch); float[] widths = { 25f, 75f }; // percentages PdfPTable headTable = new PdfPTable(widths); headTable.setWidthPercentage(100); headTable.getDefaultCell().setBorder(Rectangle.NO_BORDER); headTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell = new PdfPCell(image); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); headTable.addCell(cell); Phrase phrase = new Phrase(); ch = new Chunk("Printed Market Bucks Audit Sheet\n ", fntb2); phrase.add(ch); Paragraph pp = new Paragraph(); pp.setIndentationLeft(20); pp.setAlignment(Element.ALIGN_LEFT); pp.add(phrase); cell = new PdfPCell(pp); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); headTable.addCell(cell); document.add(headTable); // document.add(spacePhrase); document.add(spacePhrase); // float[] widths3 = { 50f, 50f }; // percentages PdfPTable table = new PdfPTable(widths3); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); phrase = new Phrase(); ch = new Chunk(" Date: ", fntb); phrase.add(ch); ch = new Chunk(Helper.getToday() + "\n\n\n", fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk(" ", fntb); // empty cell phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); // document.add(table); // phrase = new Phrase(new Chunk("\n", fnt)); document.add(phrase); // float[] widths2 = { 15f, 10f, 15f, 15f, 15f, 15f, 15f }; // percentages table = new PdfPTable(widths2); table.setWidthPercentage(90); // table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); // table.getDefaultCell().setPadding(5); phrase = new Phrase(); ch = new Chunk("Count of MB/GC", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Value", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Total Dollars", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Printed By", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Start Number", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("End Number", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("Printed Date", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // // next rows // for (Batch one : batches) { phrase = new Phrase(); ch = new Chunk(one.getBatch_size(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(1f); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("$" + one.getValue() + ".00", fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(1f); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk(moneyFormat.format(one.getTotal()), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(1f); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk(one.getUser().toString(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(1f); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk(one.getStart_seq(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(1f); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk(one.getEnd_seq(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(1f); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk(one.getDate(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(1f); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); } document.add(table); // document.add(spacePhrase); document.add(spacePhrase); // ch = new Chunk("Audit By:____________________________________________________________________\n\n", fnt); phrase = new Phrase(); phrase.add(ch); document.add(phrase); ch = new Chunk(" :_______________________________________)_____________________________\n\n", fnt); phrase = new Phrase(); phrase.add(ch); document.add(phrase); ch = new Chunk(" :_____________________________________________________________________\n\n", fnt); phrase = new Phrase(); phrase.add(ch); document.add(phrase); // document.add(spacePhrase); document.add(spacePhrase); // document.close(); writer.close(); res.setHeader("Expires", "0"); res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); res.setHeader("Pragma", "public"); // // setting the content type res.setHeader("Content-Disposition", " attachment; filename=" + filename); res.setContentType("application/pdf"); // // the contentlength is needed for MSIE!!! res.setContentLength(baos.size()); // out = res.getOutputStream(); if (out != null) { baos.writeTo(out); } } catch (Exception ex) { System.err.println(ex); } }
From source file:bucks.GenerateChecks.java
License:Open Source License
void generate_mb(Document document, int seq, String value, Image image, PdfWriter writer) { try {/*from ww w.j a va 2s. c o m*/ float[] widths = { 15f, 40f, 45f }; // percentages PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.setSpacingAfter(0f); table.setSpacingBefore(0f); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); PdfPCell cell = new PdfPCell(image); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_TOP); // cell.setFixedHeight(46f); table.addCell(cell); Phrase phrase = new Phrase(); Chunk ch = new Chunk("FARMERS' MARKET BUCKS\n", fntb2); phrase.add(ch); ch = new Chunk("Parks & Recreation\nBloomington, IN", fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Issue Date___________ ", fnt10); phrase.add(ch); ch = new Chunk("No. " + seq, fnt10); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); table.addCell(cell); // // document.add(table); // phrase = new Phrase(); ch = new Chunk("\nThis certificate is good for ", fnt); phrase.add(ch); ch = new Chunk("$" + value + ".00 ", fntb2); phrase.add(ch); ch = new Chunk("towards the purchase of ", fnt); phrase.add(ch); ch = new Chunk("eligible food items ", fntb); phrase.add(ch); ch = new Chunk("at the Bloomington Community Farmers' Market. ", fnt); phrase.add(ch); ch = new Chunk("No change allowed. \n", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setFixedHeight(48f); cell.setColspan(3); table.addCell(cell); document.add(table); // float[] widths2 = { 60f, 25f, 10f, 5f }; table = new PdfPTable(widths2); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); //table.getDefaultCell().setPadding(0); table.setSpacingAfter(0f); table.setSpacingBefore(0f); // phrase = new Phrase(); ch = new Chunk( "Customers shall redeem this certificate at Farmers' Market\nby December 1st of the year issued.", fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setColspan(2); cell.setFixedHeight(81f); table.addCell(cell); // Barcode barcode = BarcodeFactory.createCode39("" + seq, false); // barcode text has problem, so we turned off // and decided to add it ourselves. barcode.setDrawingText(false); // barcode.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 16)); // barcode.setLabel(""+seq); BufferedImage bi = BarcodeImageHandler.getImage(barcode); /* int width = bi.getWidth(); int height = bi.getHeight(); Graphics2D g2 = bi.createGraphics(); g2.setColor(Color.BLACK); g2.drawString(""+seq, 60, 70); g2.dispose(); */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); // // added this hack to get rid of underline in the barcode // this worked on windows, but the text did not show up on linux // that is why we decided to abandon the text all the way // and add it ourselves /* BarcodeImageHandler.writePNG(barcode, baos); File bcImg = File.createTempFile("bc-", ".png"); bcImg.deleteOnExit(); byte[] imageBytes = baos.toByteArray(); FileOutputStream fos = new FileOutputStream(bcImg); BarcodeImageHandler.writePNG(barcode, fos); */ // // old ImageIO.write(bi, "png", baos); byte[] imageBytes = baos.toByteArray(); // Image barCodeImage = Image.getInstance(imageBytes); barCodeImage.setRotationDegrees(90f); // barCodeImage.scalePercent(35f); barCodeImage.scalePercent(35f, 50f); // float widthf = barCodeImage.getWidth(); // float heightf = barCodeImage.getHeight(); cell = new PdfPCell(barCodeImage); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(0); cell.setLeading(0f, 0f); // cell.setFixedHeight(81f); table.addCell(cell); // // the barcode text that we added underneath the barcode // int textWidth = 53, textHeight = 50; BufferedImage bi2 = new BufferedImage(textWidth, textHeight, BufferedImage.TYPE_BYTE_BINARY); Graphics2D g = bi2.createGraphics(); g.setFont(new java.awt.Font("Verdana", java.awt.Font.PLAIN, 10)); g.setColor(Color.WHITE); g.fillRect(0, 0, textWidth, textHeight); g.setColor(Color.BLACK); g.drawString("" + seq, 5, 10); g.dispose(); baos = new ByteArrayOutputStream(); ImageIO.write(bi2, "png", baos); imageBytes = baos.toByteArray(); Image barCodeTextImage = Image.getInstance(imageBytes); barCodeTextImage.setRotationDegrees(90f); cell = new PdfPCell(barCodeTextImage); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(0); cell.setLeading(0f, 0f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("\nThis certificate is not refundable and cannot be replaced if lost or stolen.\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setFixedHeight(34f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("\nMust have official stamp to be valid.\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setColspan(3); table.addCell(cell); document.add(table); // float[] widths4 = { 40f, 60f }; // percentages table = new PdfPTable(widths4); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.setSpacingBefore(0f); table.setSpacingAfter(0f); phrase = new Phrase(); ch = new Chunk("APPROVED BY THE STATE BOARD OF ACCOUNTS\nFOR THE CITY OF BLOOMINGTON, IN 2007\n\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(40f); // 40 table.addCell(cell); phrase = new Phrase(); ch = new Chunk( "VENDERS SHALL REDEEM THIS CERTIFICATE THROUGH BLOOMINGTON'S\nPARKS & REC DEPT. BY DECEMBER 15TH OF THE YEAR ISSUED\n\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); document.add(table); } catch (Exception ex) { System.err.println(ex); } }
From source file:bucks.GenerateChecks.java
License:Open Source License
void generate_gc(Document document, int seq, String value, Image image, PdfWriter writer) { try {//from ww w. j a va2 s . c om float[] widths = { 13f, 54f, 33f }; // percents PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.setSpacingAfter(0f); table.setSpacingBefore(0f); PdfPCell cell = new PdfPCell(image); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setFixedHeight(46f); table.addCell(cell); Phrase phrase = new Phrase(); Chunk ch = new Chunk("FARMERS' MARKET GIFT CERTIFICATE\n", fntb2); phrase.add(ch); ch = new Chunk("Parks & Recreation\nBloomington, IN", fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Issue Date__________ ", fnt10); phrase.add(ch); ch = new Chunk("No. " + seq, fnt10); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("\nThis certificate is good for ", fnt); phrase.add(ch); ch = new Chunk("$" + value + ".00 ", fntb2); phrase.add(ch); ch = new Chunk("towards the purchase of ", fnt); phrase.add(ch); ch = new Chunk("products from ", fnt); phrase.add(ch); ch = new Chunk("the Bloomington Community Farmers' Market and A Fair of The Arts Vendors. ", fnt); phrase.add(ch); ch = new Chunk("Change may be given. \n", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setColspan(3); cell.setFixedHeight(48f); table.addCell(cell); document.add(table); // float[] widths2 = { 60f, 25f, 10f, 5f }; table = new PdfPTable(widths2); table.setWidthPercentage(100); table.setSpacingBefore(0f); table.setSpacingAfter(0f); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); // phrase = new Phrase(); ch = new Chunk("Customers shall redeem this certificate at Market\nwithin one year of date issued. ", fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setColspan(2); cell.setFixedHeight(81f); table.addCell(cell); Barcode barcode = BarcodeFactory.createCode39("" + seq, true); barcode.setDrawingText(false); BufferedImage bi = BarcodeImageHandler.getImage(barcode); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // ImageIO.write(bi, "png", baos); byte[] imageBytes = baos.toByteArray(); // Image barCodeImage = Image.getInstance(imageBytes); barCodeImage.setRotationDegrees(90); // barCodeImage.scalePercent(35f); barCodeImage.scalePercent(35f, 50f); cell = new PdfPCell(barCodeImage); // cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(0); cell.setLeading(0f, 0f); table.addCell(cell); // // adding text underneath the barcode // int textWidth = 53, textHeight = 50; BufferedImage bi2 = new BufferedImage(textWidth, textHeight, BufferedImage.TYPE_BYTE_BINARY); Graphics2D g = bi2.createGraphics(); g.setFont(new java.awt.Font("Verdana", java.awt.Font.PLAIN, 10)); g.setColor(Color.WHITE); g.fillRect(0, 0, textWidth, textHeight); g.setColor(Color.BLACK); g.drawString("" + seq, 5, 10); g.dispose(); baos = new ByteArrayOutputStream(); ImageIO.write(bi2, "png", baos); imageBytes = baos.toByteArray(); Image barCodeTextImage = Image.getInstance(imageBytes); barCodeTextImage.setRotationDegrees(90f); cell = new PdfPCell(barCodeTextImage); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(0); cell.setLeading(0f, 0f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("\nThis certificate is not refundable and cannot be replaced if lost or stolen.\n\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setFixedHeight(30f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("\nMust have official stamp to be valid.\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setColspan(3); table.addCell(cell); document.add(table); // float[] widths3 = { 40f, 60f }; // percentages table = new PdfPTable(widths3); table.setWidthPercentage(100); table.setSpacingBefore(0f); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); phrase = new Phrase(); ch = new Chunk("APPROVED BY THE STATE BOARD OF ACCOUNTS\nFOR THE CITY OF BLOOMINGTON, IN 2007\n\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); // cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setFixedHeight(44f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk( "VENDERS SHALL REDEEM THIS CERTIFICATE THROUGH BLOOMINGTON'S\nPARKS & REC DEPT. BY DECEMBER 15TH OF THE YEAR RECEIVED\n\n\n", fnts); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); document.add(table); // document.add( Chunk.NEWLINE ); } catch (Exception ex) { System.err.println(ex); } }
From source file:bucks.RedeemInvoice.java
License:Open Source License
void generateInvoice(HttpServletResponse res, Redeem redeem) { ///*from w ww . j a v a 2 s. c o m*/ // paper size legal (A4) 8.5 x 11 // page 1-inch = 72 points // String spacer = " \n"; User user = redeem.getUser(); Vendor vendor = redeem.getVendor(); List<Buck> bk_bucks = redeem.getBk_bucks(); List<Buck> gc5_bucks = redeem.getGc5_bucks(); List<Buck> gc20_bucks = redeem.getGc20_bucks(); List<Buck> gc_bucks = redeem.getGc_bucks(); List<Dispute> disputes = redeem.getDisputes(); int bk_total = 0, gc5_total = 0, gc20_total = 0, total = 0; if (bk_bucks.size() > 0) { bk_total = bk_bucks.size() * 3; } if (gc5_bucks.size() > 0) { gc5_total = gc5_bucks.size() * 5; } if (gc20_bucks.size() > 0) { gc20_total = gc20_bucks.size() * 20; } total = bk_total + gc5_total + gc20_total; String vendor_name = ""; if (vendor != null) { vendor_name = vendor.getCleanName(); } ServletOutputStream out = null; String filename = "invoice_" + vendor_name + "_" + redeem.getId() + ".pdf"; try { // space // Rectangle pageSize = new Rectangle(612, 792); // 8.5" X 11" Document document = new Document(pageSize, 36, 36, 18, 18);// 18,18,54,35 ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); Image image = Image.getInstance(imageUrl); // image = Image.getInstance(byte bytes[]);// generated image image.scalePercent(20f); Font fnt = new Font(Font.TIMES_ROMAN, 12, Font.NORMAL); Font fntb = new Font(Font.TIMES_ROMAN, 12, Font.BOLD); Font fntb2 = new Font(Font.TIMES_ROMAN, 14, Font.BOLD); // Phrase spacePhrase = new Phrase(); Chunk ch = new Chunk(spacer, fnt); spacePhrase.add(ch); float[] widths = { 15f, 85f }; // percentages PdfPTable headTable = new PdfPTable(widths); headTable.setWidthPercentage(100); headTable.getDefaultCell().setBorder(Rectangle.NO_BORDER); headTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); // image.setWidthPercentage(15.0f); PdfPCell cell = new PdfPCell(image); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); // cell.setFixedHeight(46f); headTable.addCell(cell); Phrase phrase = new Phrase(); ch = new Chunk( "City of Bloomington Community Farmers Market\n Gift Certificates/Market Bucks Invoice\n ", fntb2); phrase.add(ch); Paragraph pp = new Paragraph(); pp.setIndentationLeft(20); pp.setAlignment(Element.ALIGN_LEFT); pp.add(phrase); cell = new PdfPCell(pp); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); headTable.addCell(cell); document.add(headTable); // float[] widths3 = { 50f, 50f }; // percentages PdfPTable table = new PdfPTable(widths3); table.setWidthPercentage(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); phrase = new Phrase(); ch = new Chunk(" Vendor Name: ", fntb); phrase.add(ch); ch = new Chunk(vendor.getFullName(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Date: ", fntb); phrase.add(ch); ch = new Chunk(redeem.getDate(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); phrase = new Phrase(); ch = new Chunk(" Vendor Number: ", fntb); phrase.add(ch); ch = new Chunk(vendor.getId(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); phrase = new Phrase(); // empty cell ch = new Chunk(" ", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); table.addCell(cell); // extra space table.addCell(cell); document.add(table); // phrase = new Phrase(new Chunk("\n", fnt)); document.add(phrase); // float[] widths2 = { 30f, 25f, 15f, 15f, 15f }; // percentages table = new PdfPTable(widths2); table.setWidthPercentage(90); // table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); // table.getDefaultCell().setPadding(5); phrase = new Phrase(); ch = new Chunk("Type of Voucher", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("City Account Number", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("Quantity", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("Multiply", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("Value", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setBorderWidth(2f); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // // second row phrase = new Phrase(); ch = new Chunk("Market Bucks", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("201-18-1865003-47240", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); // phrase = new Phrase(); String str = " "; if (bk_bucks.size() > 0) { str = "" + bk_bucks.size(); } ch = new Chunk(str, fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setPadding(4); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("x $3.00 = ", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); phrase = new Phrase(); str = " "; if (bk_total > 0) { str = "$" + bk_total + ".00"; } ch = new Chunk(str, fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setPadding(4); table.addCell(cell); // // 3rd row phrase = new Phrase(); ch = new Chunk("$5 Gift Cerificates", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("201-18-1865003-47230", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); // phrase = new Phrase(); str = " "; if (gc5_bucks.size() > 0) { str = "" + gc5_bucks.size(); } ch = new Chunk(str, fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setPadding(4); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("x $5.00 = ", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); phrase = new Phrase(); str = " "; if (gc5_total > 0) { str = "$" + gc5_total + ".00"; } ch = new Chunk(str, fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setPadding(4); table.addCell(cell); // // 4th row phrase = new Phrase(); ch = new Chunk("$20 Gift Certificates", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("201-18-1865003-47230", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); // phrase = new Phrase(); str = " "; if (gc20_bucks.size() > 0) { str = "" + gc20_bucks.size(); } ch = new Chunk(str, fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setPadding(4); table.addCell(cell); phrase = new Phrase(); ch = new Chunk("x $20.00 = ", fnt); phrase.add(ch); cell = new PdfPCell(phrase); table.addCell(cell); str = " "; if (gc20_total > 0) { str = "$" + gc20_total + ".00"; } phrase = new Phrase(); ch = new Chunk(str, fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setPadding(4); table.addCell(cell); // // 5th row total phrase = new Phrase(); ch = new Chunk(" Total Value: ", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setColspan(4); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(2f); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk("$" + total + ".00", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(2f); cell.setPadding(4); cell.setLeading(2f, 1.2f); table.addCell(cell); document.add(table); // // float[] withs5 = { 100f }; table = new PdfPTable(withs5); table.setWidthPercentage(100); cell = new PdfPCell(new Phrase(" ")); cell.setBorder(Rectangle.BOTTOM); // cell.setBorderColorBottom(Color.BLACK); cell.setBorderWidthBottom(2f); table.addCell(cell); document.add(table); // ch = new Chunk("\n", fnt); phrase = new Phrase(); phrase.add(ch); document.add(phrase); // // float[] widths3 = {50f, 50f}; // percentages table = new PdfPTable(widths3); table.setWidthPercentage(100); // table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); phrase = new Phrase(); ch = new Chunk(" Data Entry Completed by: ", fntb); phrase.add(ch); ch = new Chunk(user.getFullName(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); // phrase = new Phrase(); ch = new Chunk(" Invoice Number: ", fntb); phrase.add(ch); ch = new Chunk(redeem.getId(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); document.add(table); document.add(spacePhrase); document.add(spacePhrase); // // adding bucks and GC // float[] widths4 = { 100f }; // percentages table = new PdfPTable(widths4); table.setWidthPercentage(100); // table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); int row = 1, col = 1; if (bk_bucks.size() > 0) { phrase = new Phrase(); ch = new Chunk(" Market Bucks ", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(2f); table.addCell(cell); phrase = new Phrase(); for (Buck one : bk_bucks) { ch = new Chunk("" + one.getId() + " ", fntb); phrase.add(ch); col++; if (col > 15) { row++; col = 1; cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); phrase = new Phrase(); } } if (col > 1) { cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); } if (row < 10) { for (int j = 0; j < 2; j++) { phrase = new Phrase(); ch = new Chunk(" ", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); row++; } } } col = 1; if (gc_bucks.size() > 0) { phrase = new Phrase(); ch = new Chunk(" Gift Certificates ", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(2f); table.addCell(cell); phrase = new Phrase(); for (Buck one : gc_bucks) { ch = new Chunk("" + one.getId() + " ", fntb); phrase.add(ch); col++; if (col > 15) { row++; col = 1; cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); phrase = new Phrase(); } } if (col > 1) { cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); } if (row < 15) { for (int j = 0; j < 2; j++) { phrase = new Phrase(); ch = new Chunk(" ", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); row++; } } } if (disputes != null && disputes.size() > 0) { phrase = new Phrase(); ch = new Chunk(" Disputed Market Bucks and/or Gift Certificates (number: reason)", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(2f); table.addCell(cell); row++; for (Dispute one : disputes) { phrase = new Phrase(); ch = new Chunk(one.getBuck_id() + ": " + one.getReason(), fnt); phrase.add(ch); if (one.hasNotes()) { ch = new Chunk("\nNotice: " + one.getNotes(), fnt); phrase.add(ch); row++; } cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); row++; } } if (redeem.hasNotes()) { phrase = new Phrase(); ch = new Chunk("\nNotice: " + redeem.getNotes(), fnt); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); row++; } if (row < 18) { for (int j = row; j < 18; j++) { phrase = new Phrase(); ch = new Chunk(" ", fntb); phrase.add(ch); cell = new PdfPCell(phrase); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(cell); } } document.add(table); document.close(); writer.close(); res.setHeader("Expires", "0"); res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); res.setHeader("Pragma", "public"); // // setting the content type res.setHeader("Content-Disposition", " attachment; filename=" + filename); res.setContentType("application/pdf"); // // the contentlength is needed for MSIE!!! res.setContentLength(baos.size()); // out = res.getOutputStream(); if (out != null) { baos.writeTo(out); } } catch (Exception ex) { System.err.println(ex); } }
From source file:ca.sqlpower.architect.profile.output.ProfilePDFFormat.java
License:Open Source License
/** * @param widths The maximum width of each column's contents in * points. THIS ARRAY WILL BE MODIFIED to the width of the widest * single word in the heading if it is wider than the existing * width value for that column. Words are split using the default * settings for java.util.StringTokenizer. * @param headerTopNColumns reference to the null count/% inner table in the header * @param headerValueColumns reference to the unique count/% inner table in the header * @param headerLengthColumns reference to the length min/max/avg inner table in the header * @param headerUniqueColumns reference to the value min/max/avg inner table in the header * @param headerNullColumns reference to the top N Value/count inner table in the header * we will resert widths of these inner table after we have all rows *///from ww w . j ava 2 s . c o m private void addHeaderRow(TableProfileResult result, ProfileTableStructure profile, BaseFont bf, float titleFSize, float colHeadingFSize, float[] widths) throws DocumentException, IOException, SQLObjectException { int ncols = headings.length; Font titleFont = new Font(bf, titleFSize, Font.BOLD); Font colHeadingFont = new Font(bf, colHeadingFSize); PdfPTable table = profile.getMainTable(); SQLTable sqlTable = result.getProfiledObject(); // TableProfileResult tProfile = (TableProfileResult) pm.getResult(sqlTable); PdfPTable infoTable = new PdfPTable(2); StringBuffer heading = new StringBuffer(); heading.append("Connection: ").append(sqlTable.getParentDatabase().getName()).append("\n"); heading.append("Table: ").append(SQLObjectUtils.toQualifiedName(sqlTable, SQLDatabase.class)); if (result.getException() != null) { heading.append("\nProfiling Error"); if (result.getException() != null) { heading.append(":\n").append(result.getException()); StackTraceElement[] stackTrace = result.getException().getStackTrace(); for (int i = 0; i < STACK_TRACE_LENGTH && i < stackTrace.length; i++) { StackTraceElement element = stackTrace[i]; heading.append("\n ").append(element.getFileName()).append(".").append(element.getClassName()) .append(".").append(element.getMethodName()).append("(").append(element.getLineNumber()) .append(")"); } if (stackTrace.length > STACK_TRACE_LENGTH) { heading.append("\n ... ").append(stackTrace.length).append(" more"); } } } else { PdfPCell infoCell; infoCell = new PdfPCell(new Phrase("Row Count:", colHeadingFont)); infoCell.setBorder(Rectangle.NO_BORDER); infoCell.setHorizontalAlignment(Element.ALIGN_RIGHT); infoTable.addCell(infoCell); infoCell = new PdfPCell(new Phrase(String.valueOf(result.getRowCount()), colHeadingFont)); infoCell.setBorder(Rectangle.NO_BORDER); infoTable.addCell(infoCell); infoCell = new PdfPCell(new Phrase("Create Date:", colHeadingFont)); infoCell.setBorder(Rectangle.NO_BORDER); infoCell.setHorizontalAlignment(Element.ALIGN_RIGHT); infoTable.addCell(infoCell); DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); infoCell = new PdfPCell(new Phrase(df.format(new Date(result.getCreateStartTime())), colHeadingFont)); infoCell.setBorder(Rectangle.NO_BORDER); infoTable.addCell(infoCell); infoCell = new PdfPCell(new Phrase("Elapsed:", colHeadingFont)); infoCell.setBorder(Rectangle.NO_BORDER); infoCell.setHorizontalAlignment(Element.ALIGN_RIGHT); infoTable.addCell(infoCell); infoCell = new PdfPCell(new Phrase(result.getTimeToCreate() + "ms", colHeadingFont)); infoCell.setBorder(Rectangle.NO_BORDER); infoTable.addCell(infoCell); } PdfPCell hcell = new PdfPCell(new Phrase(heading.toString(), titleFont)); hcell.setColspan(ncols - 3); hcell.setBorder(Rectangle.NO_BORDER); hcell.setVerticalAlignment(Element.ALIGN_BOTTOM); table.addCell(hcell); hcell = new PdfPCell(infoTable); hcell.setColspan(3); hcell.setBorder(Rectangle.NO_BORDER); table.addCell(hcell); if (sqlTable.getColumns().size() > 0) { int colNo = 0; // column name Phrase colTitle = new Phrase("Column Name", colHeadingFont); PdfPCell cell = new PdfPCell(colTitle); cell.setBorder(Rectangle.BOTTOM | Rectangle.TOP); cell.setBorderWidth(2); cell.setBackgroundColor(new Color(200, 200, 200)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); // ensure column width is at least enough for widest word in heading widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; // date type colTitle = new Phrase("Data Type", colHeadingFont); cell = new PdfPCell(colTitle); cell.setBorder(Rectangle.BOTTOM | Rectangle.TOP); cell.setBorderWidth(2); cell.setBackgroundColor(new Color(200, 200, 200)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; // null count and % colTitle = new Phrase("NULL", colHeadingFont); cell = new PdfPCell(colTitle); cell.setColspan(2); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableNullColumn().addCell(cell); colTitle = new Phrase("#", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableNullColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; colTitle = new Phrase("%", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableNullColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; cell = new PdfPCell(profile.getInnerTableNullColumn()); cell.setColspan(2); cell.setBackgroundColor(new Color(200, 200, 200)); cell.setBorderWidth(2); cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM); table.addCell(cell); // unique count and % colTitle = new Phrase("Unique", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(2); profile.getInnerTableUniqueColumn().addCell(cell); colTitle = new Phrase("#", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableUniqueColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; colTitle = new Phrase("%", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableUniqueColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; cell = new PdfPCell(profile.getInnerTableUniqueColumn()); cell.setColspan(2); cell.setBackgroundColor(new Color(200, 200, 200)); cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM); cell.setBorderWidth(2); table.addCell(cell); // length max/min/avg colTitle = new Phrase("Length", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(3); profile.getInnerTableLengthColumn().addCell(cell); colTitle = new Phrase("Min", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableLengthColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; colTitle = new Phrase("Max", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableLengthColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; colTitle = new Phrase("Avg", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableLengthColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; cell = new PdfPCell(profile.getInnerTableLengthColumn()); cell.setColspan(3); cell.setBackgroundColor(new Color(200, 200, 200)); cell.setBorderWidth(2); cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM); table.addCell(cell); // value max/min/avg colTitle = new Phrase("Value", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(3); profile.getInnerTableValueColumn().addCell(cell); colTitle = new Phrase("Min", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableValueColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; colTitle = new Phrase("Max", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableValueColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; colTitle = new Phrase("Avg", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableValueColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; cell = new PdfPCell(profile.getInnerTableValueColumn()); cell.setColspan(3); cell.setBackgroundColor(new Color(200, 200, 200)); cell.setBorderWidth(2); cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM); table.addCell(cell); // top n colTitle = new Phrase("Top N", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(2); profile.getInnerTableTopNColumn().addCell(cell); colTitle = new Phrase("Values", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableTopNColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; colTitle = new Phrase("#", colHeadingFont); cell = new PdfPCell(colTitle); cell.setHorizontalAlignment(Element.ALIGN_CENTER); profile.getInnerTableTopNColumn().addCell(cell); widths[colNo] = Math.max(widths[colNo], bf.getWidthPoint(colTitle.content(), colHeadingFSize)); colNo++; cell = new PdfPCell(profile.getInnerTableTopNColumn()); cell.setColspan(2); cell.setBackgroundColor(new Color(200, 200, 200)); cell.setBorderWidth(2); cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM); table.addCell(cell); } else { hcell = new PdfPCell(new Phrase("No Column Found in the table", titleFont)); hcell.setColspan(ncols); hcell.setBorder(Rectangle.BOTTOM); hcell.setVerticalAlignment(Element.ALIGN_LEFT); table.addCell(hcell); } table.setHeaderRows(2); }
From source file:candelaria.presentacion.beans.DetalleFacturaControlador.java
public void imprimirFac() { //DateFormat dfDateFull = DateFormat.getDateInstance(DateFormat.FULL); try {//from w w w. ja v a 2 s .c o m //Generamos el archivo PDF String directorioArchivos; ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); directorioArchivos = ctx.getRealPath("/") + "reports"; String name = directorioArchivos + "/document-factura.pdf"; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(name)); //PdfWriter writer = PdfWriter.getInstance(document, //new FileOutputStream("C:")); Paragraph paragraph = new Paragraph(); Paragraph paragraph1 = new Paragraph(); PdfPTable table = new PdfPTable(4); PdfPTable table1 = new PdfPTable(2); PdfPTable table2 = new PdfPTable(1); PdfPTable table3 = new PdfPTable(2); PdfPTable table5 = new PdfPTable(4); PdfPTable tablaF = new PdfPTable(1); PdfPTable tablaF1 = new PdfPTable(3); PdfPTable tablaF2 = new PdfPTable(3); paragraph.add("\n\n\n"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("YUQUI OLGA"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("\n"); paragraph.add("Dir. La Candelaria Barrio Nuevo"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("\n"); paragraph.add("Telf: 3014019"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("\n"); paragraph.add("Penipe - Ecuador"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("\n"); paragraph.add("\n"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("AUTORIZACION SRI __________ - RUC.: 0600750897001"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("\n"); paragraph.add("FACTURA: " + facturaSel.getId_factura()); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph.add("\n\n\n"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph1.add("\n\n"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); document.open(); //primera linea PdfPCell cell5 = new PdfPCell(new Paragraph("Fecha: " + fecha_cambiada)); //PdfPCell cell6 = new PdfPCell(new Paragraph("Factura #: " + facturaSel.getId_factura())); PdfPCell cell7 = new PdfPCell(new Paragraph("Cedula: " + facturaSel.getId_cliente().getRuc_cliente())); //segunda linea PdfPCell cell8 = new PdfPCell( new Paragraph("Nombre Cliente: " + facturaSel.getId_cliente().getNombres_cliente() + facturaSel.getId_cliente().getApellidos_cliente())); //tercera fila PdfPCell cell9 = new PdfPCell( new Paragraph("Direccin: " + facturaSel.getId_cliente().getDireccion_cliente())); PdfPCell cell10 = new PdfPCell( new Paragraph("Tlefono: " + facturaSel.getId_cliente().getTelefono_cliente())); PdfPCell cellf1 = new PdfPCell(new Paragraph("SubTotal " + totalHoja)); PdfPCell cellf2 = new PdfPCell(new Paragraph("Impuesto Iva " + impuestoFactura)); PdfPCell cellf21 = new PdfPCell(new Paragraph("___________________")); PdfPCell cellf22 = new PdfPCell(new Paragraph("___________________")); PdfPCell cellf3 = new PdfPCell(new Paragraph("TOTAL " + totalFactura)); PdfPCell cellf31 = new PdfPCell(new Paragraph("FIRMA AUTORIZADA")); PdfPCell cellf32 = new PdfPCell(new Paragraph("FIRMA CLIENTE")); PdfPCell cell11 = new PdfPCell(new Paragraph("Cantidad")); PdfPCell cell12 = new PdfPCell(new Paragraph("Descripcin")); PdfPCell cell13 = new PdfPCell(new Paragraph("V. Unitario")); PdfPCell cell14 = new PdfPCell(new Paragraph("V. Total")); cell5.setHorizontalAlignment(Element.ALIGN_LEFT); //cell6.setHorizontalAlignment(Element.ALIGN_CENTER); cell7.setHorizontalAlignment(Element.ALIGN_RIGHT); cellf1.setHorizontalAlignment(Element.ALIGN_RIGHT); cellf2.setHorizontalAlignment(Element.ALIGN_RIGHT); cellf3.setHorizontalAlignment(Element.ALIGN_RIGHT); cellf21.setHorizontalAlignment(Element.ALIGN_CENTER); cellf31.setHorizontalAlignment(Element.ALIGN_CENTER); cellf22.setHorizontalAlignment(Element.ALIGN_CENTER); cellf32.setHorizontalAlignment(Element.ALIGN_CENTER); cell8.setHorizontalAlignment(Element.ALIGN_LEFT); cell9.setHorizontalAlignment(Element.ALIGN_LEFT); cell10.setHorizontalAlignment(Element.ALIGN_RIGHT); cellf21.setBorder(Rectangle.NO_BORDER); cellf31.setBorder(Rectangle.NO_BORDER); cellf22.setBorder(Rectangle.NO_BORDER); cellf32.setBorder(Rectangle.NO_BORDER); cell5.setBorder(Rectangle.NO_BORDER); //cell6.setBorder(Rectangle.NO_BORDER); cell7.setBorder(Rectangle.NO_BORDER); cell8.setBorder(Rectangle.NO_BORDER); cell9.setBorder(Rectangle.NO_BORDER); cell10.setBorder(Rectangle.NO_BORDER); //cell7.setBorder(Rectangle.NO_BORDER); //cell8.setBorder(Rectangle.NO_BORDER); cell11.setBorder(Rectangle.NO_BORDER); cell12.setBorder(Rectangle.NO_BORDER); cell13.setBorder(Rectangle.NO_BORDER); cell14.setBorder(Rectangle.NO_BORDER); cellf1.setBorder(Rectangle.NO_BORDER); cellf2.setBorder(Rectangle.NO_BORDER); cellf3.setBorder(Rectangle.NO_BORDER); cell11.setHorizontalAlignment(Element.ALIGN_LEFT); cell12.setHorizontalAlignment(Element.ALIGN_LEFT); cell13.setHorizontalAlignment(Element.ALIGN_RIGHT); cell14.setHorizontalAlignment(Element.ALIGN_RIGHT); table1.addCell(cell5); //table1.addCell(cell6); table1.addCell(cell7); //aadir segunda fila table2.addCell(cell8); //aadir tercera fila table3.addCell(cell9); table3.addCell(cell10); //aadir cuarta fila table5.addCell(cell11); table5.addCell(cell12); table5.addCell(cell13); table5.addCell(cell14); tablaF.addCell(cellf1); tablaF1.addCell(cellf21); tablaF1.addCell(cellf22); tablaF1.addCell(cellf2); tablaF2.addCell(cellf31); tablaF2.addCell(cellf32); tablaF2.addCell(cellf3); for (int x = 0; x < lstDetalleFactura.size(); x++) { PdfPCell cell1 = new PdfPCell(new Paragraph("" + lstDetalleFactura.get(x).getCantidad())); PdfPCell cell2 = new PdfPCell(new Paragraph( "" + lstDetalleFactura.get(x).getId_producto().getId_categoria().getNombre_producto())); PdfPCell cell3 = new PdfPCell(new Paragraph( "" + lstDetalleFactura.get(x).getId_producto().getId_categoria().getPrecio_producto())); PdfPCell cell4 = new PdfPCell(new Paragraph("" + lstDetalleFactura.get(x).getValor_total())); /* Chunk chunk = new Chunk( "\n" + lstDetalles.get(x).getCantidadDet() + " " + lstDetalles.get(x).getIdSer().getNombreSer() + " " + lstDetalles.get(x).getIdSer().getPrecioSer() + " " + lstDetalles.get(x).getPrecio());*/ cell1.setBorder(Rectangle.NO_BORDER); cell2.setBorder(Rectangle.NO_BORDER); cell3.setBorder(Rectangle.NO_BORDER); cell4.setBorder(Rectangle.NO_BORDER); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell2.setHorizontalAlignment(Element.ALIGN_LEFT); cell3.setHorizontalAlignment(Element.ALIGN_RIGHT); cell4.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setMinimumHeight(10f); cell2.setMinimumHeight(5f); table.setTotalWidth(100f); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); //aadir primera fila table.setSpacingBefore(30f); table.setSpacingAfter(50f); //paragraph4.add(chunk); //paragraph4.setAlignment(Paragraph.ALIGN_JUSTIFIED_ALL); } document.add(paragraph); document.add(table1); document.add(table2); document.add(table3); document.add(paragraph1); document.add(table5); document.add(table); document.add(tablaF); document.add(tablaF1); document.add(tablaF2); //document.setFooter(event); document.close(); //---------------------------- //Abrimos el archivo PDF FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/pdf"); response.setHeader("Content-disposition", "inline=filename=" + name); try { response.getOutputStream().write(Util.getBytesFromFile(new File(name))); response.getOutputStream().flush(); response.getOutputStream().close(); context.responseComplete(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:ch.gpb.elexis.cst.view.CstResultPart.java
License:Open Source License
private void makeActions(final Control viewer) { actionScreenshot = new Action() { public void run() { if (profile == null) { SWTHelper.alert("No profile", "Ohne Profil kann kein Resultat erzeugt werden"); return; }// ww w .j a va 2s . c o m GC gc = null; Image image = null; try { String latestPath = CoreHub.userCfg.get(CstPreference.CST_IDENTIFIER_LATESTPATH, null); if (latestPath == null) { latestPath = System.getProperty("user.home"); } FileDialog fd = new FileDialog(baseComposite.getShell(), SWT.SAVE); fd.setText("Save"); fd.setFilterPath(latestPath); String[] filterExt = { "*.png", "*.*" }; fd.setFilterExtensions(filterExt); fd.setFileName(CstService.generateFilename(patient)); String selected = fd.open(); if (selected == null) { return; } File selFile = new File(selected); CoreHub.userCfg.set(CstPreference.CST_IDENTIFIER_LATESTPATH, selFile.getParentFile().getAbsolutePath()); //if (profile.getAnzeigeTyp().toLowerCase().equals("effektiv")) { if (profile.getAnzeigeTyp().toLowerCase().equals(CstProfile.ANZEIGETYP_EFFEKTIV)) { if (profile.getAusgabeRichtung()) { image = new Image(viewer.getDisplay(), 1123, viewer.getBounds().height); } else { image = new Image(viewer.getDisplay(), 794, viewer.getBounds().height); } } else { image = new Image(viewer.getDisplay(), 794, viewer.getBounds().height); } ImageLoader loader = new ImageLoader(); gc = new GC(image); viewer.print(gc); gc.dispose(); loader.data = new ImageData[] { image.getImageData() }; loader.save(selected, SWT.IMAGE_PNG); } catch (Exception e) { log.error("Error saving png: " + e.toString()); showMessage("Error while saving PNG", e.getMessage()); } finally { if (image != null) { image.dispose(); } if (gc != null) { gc.dispose(); } } } }; actionScreenshot.setText(Messages.Cst_Text_Save_as_png); actionScreenshot.setToolTipText(Messages.Cst_Text_Save_as_png); actionScreenshot.setImageDescriptor( PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_ELEMENT)); actionScreenshot.setImageDescriptor(Activator.getImageDescriptor(Activator.IMG_PNG_PATH)); // TODO: die pdf ausgabe ist eine ziemliche Baustelle - berarbeiten actionPdf = new Action() { public void run() { ////////////////////////// if (profile == null) { SWTHelper.alert("No profile", "Ohne Profil kann kein Resultat erzeugt werden"); return; } GC gc = null; Image image = null; try { String latestPath = CoreHub.userCfg.get(CstPreference.CST_IDENTIFIER_LATESTPATH, null); if (latestPath == null) { latestPath = System.getProperty("user.home"); } FileDialog fd = new FileDialog(baseComposite.getShell(), SWT.SAVE); fd.setText("Save"); fd.setFilterPath(latestPath); String[] filterExt = { "*.pdf", "*.*" }; fd.setFilterExtensions(filterExt); fd.setFileName(CstService.generateFilename(patient)); String selected = fd.open(); if (selected == null) { return; } File selFile = new File(selected); CoreHub.userCfg.set(CstPreference.CST_IDENTIFIER_LATESTPATH, selFile.getParentFile().getAbsolutePath()); int printHeigth = 1123; int printWidth = 794; if (profile.getAusgabeRichtung()) { printHeigth = 794; printWidth = 1123; } // get the image from the viewport image = new Image(viewer.getDisplay(), printWidth, viewer.getBounds().height); ImageLoader loader = new ImageLoader(); gc = new GC(image); viewer.print(gc); gc.dispose(); // prepare title data //Date date = new Date(); //SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm"); Patient patient = Patient.load(profile.getKontaktId()); //String sTitle = "Gemeinschaftspraxis Brunnmatt Dr. Beat Knzi "; String sTitle; sTitle = profile.getOutputHeader() == null ? "No header configured!" : profile.getOutputHeader(); if (sTitle == null || sTitle.length() == 0) { sTitle = "No header configured!"; } sTitle = sTitle + " Datum: " + CstService.getReadableDateAndTime(); // get option (paging to A4/ in one piece) int pdfOutputOption = 0; boolean onePage = true; PdfOptionsDialog dialog = new PdfOptionsDialog(baseComposite.getShell()); dialog.create(); if (dialog.open() == Window.OK) { pdfOutputOption = dialog.getPdfOutputOption(); if (pdfOutputOption == PdfOptionsDialog.OPTION_ONE_PAGE) { onePage = true; } else { onePage = false; } } float docHeight = viewer.getBounds().height; docHeight = docHeight / 7.5f; float fontSize = 12f; if (docHeight < 360f) { docHeight = 360f; } BufferedImage bimage = ImageUtils.convertToAWT(image.getImageData()); // create an Itextt Image from AWT BufferedImage com.lowagie.text.Image itextImage = null; java.awt.Image awtImage = null; try { awtImage = Toolkit.getDefaultToolkit().createImage(bimage.getSource()); itextImage = com.lowagie.text.Image.getInstance(awtImage, null); } catch (Exception e) { log.error("Error on image loading: " + e.toString()); e.printStackTrace(); } // only for debugging //loader.data = new ImageData[] { image.getImageData() }; //loader.save("C:\\Users\\daniel\\tmp\\debug.png", SWT.IMAGE_PNG); Rectangle pagesize = new Rectangle(595f, itextImage.getHeight() * 0.75f); // is it a4 quer? if (profile.getAusgabeRichtung()) { pagesize = new Rectangle(842f, itextImage.getHeight() * 0.75f); } //System.out.println("pagesize: " + pagesize.toString()); Document document; if (onePage) { document = new Document(pagesize); } else { document = new Document(PageSize.A4); if (profile.getAusgabeRichtung()) { document = new Document(PageSize.A4.rotate()); } } document.addCreationDate(); try { // creation of the different writers PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(selected)); // various fonts BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", true); BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true); BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", true); com.lowagie.text.Font fontHelv12 = new com.lowagie.text.Font(bf_helv, fontSize); com.lowagie.text.Font fontTimes = new com.lowagie.text.Font(bf_times, fontSize); fontTimes.setSize(fontSize); fontTimes.setStyle(com.lowagie.text.Font.ITALIC); Chunk chunkHeader = new Chunk(sTitle, FontFactory.getFont(FontFactory.HELVETICA, fontSize, com.lowagie.text.Font.NORMAL, new java.awt.Color(255, 0, 0))); com.lowagie.text.Phrase phraseHeader = new com.lowagie.text.Phrase(chunkHeader); // headers and footers must be added before the document // is opened Chunk chunkFooter = new Chunk("Seite: ", FontFactory.getFont(FontFactory.HELVETICA, fontSize, com.lowagie.text.Font.BOLD, new java.awt.Color(0, 0, 0))); Phrase phraseFooter = new Phrase(chunkFooter); phraseFooter.setFont(fontHelv12); HeaderFooter footer = new HeaderFooter(phraseFooter, true); footer.setBorder(Rectangle.NO_BORDER); footer.setAlignment(Element.ALIGN_CENTER); document.setFooter(footer); Phrase headerPhrase = new Phrase(sTitle); headerPhrase.setFont(fontTimes); HeaderFooter header = new HeaderFooter(phraseHeader, false); header.setBorder(Rectangle.BOTTOM); header.setBorderWidth(0.5f); header.setAlignment(Element.ALIGN_LEFT); document.setHeader(header); document.open(); //System.out.println("itext image w: " + itextImage.getWidth() + " h:" + itextImage.getHeight()); if (onePage) { int scale = 66; itextImage.scalePercent(scale); document.add(itextImage); } else { BufferedImage[] imageChunks = ImageUtils.splitImageByHeigth(bimage, printHeigth); for (int i = 0; i < imageChunks.length; i++) { com.lowagie.text.Image itextImage2 = com.lowagie.text.Image.getInstance( Toolkit.getDefaultToolkit().createImage(imageChunks[i].getSource()), null); // width becomes typically 523 (595 - 72) for a4Hoch or 770 (842 - 72) for A4Quer float imgWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(); float imgHeigth = itextImage.getHeight() * 0.75f; itextImage2.setAbsolutePosition(30, 20); int scale = 66; itextImage2.scalePercent(scale); document.add(itextImage2); document.newPage(); } } // we're done! document.close(); /////////////////////////////// } catch (Exception ex) { log.error(ex.getMessage()); showMessage("Error while generating PDF", ex.getMessage()); } } finally { if (image != null) { image.dispose(); } if (gc != null) { gc.dispose(); } /* image.dispose(); gc.dispose(); */ } } }; actionPdf.setText(Messages.Cst_Text_Save_as_pdf); actionPdf.setToolTipText(Messages.Cst_Text_Save_as_pdf); /* * actionPdf.setImageDescriptor(PlatformUI.getWorkbench() .getSharedImages() * .getImageDescriptor(ISharedImages.IMG_OBJ_FILE)); */ actionPdf.setImageDescriptor(Activator.getImageDescriptor(Activator.IMG_PDF_PATH)); }
From source file:com.aripd.clms.service.ContractServiceBean.java
@Override public void generatePdf(ContractEntity contract) { String baseFontUrl = "/fonts/Quivira.otf"; FontFactory.register(baseFontUrl);//from ww w.j av a 2 s .co m ByteArrayOutputStream output = new ByteArrayOutputStream(); try { BaseFont bf = BaseFont.createFont(baseFontUrl, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font18n = new Font(bf, 18, Font.NORMAL); Font font12n = new Font(bf, 12, Font.NORMAL); Font font8n = new Font(bf, 8, Font.NORMAL); Font font8nbu = new Font(bf, 8, Font.BOLD | Font.UNDERLINE); Font font8ng = new Font(bf, 8, Font.NORMAL, Color.DARK_GRAY); Font font6n = new Font(bf, 6, Font.NORMAL); Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, output); document.open(); addMetaData(document); addTitlePage(document, contract); Image imgBlue = Image.getInstance(1, 1, 3, 8, new byte[] { (byte) 0, (byte) 0, (byte) 255, }); imgBlue.scaleAbsolute(document.getPageSize().getWidth(), 10); imgBlue.setAbsolutePosition(0, document.getPageSize().getHeight() - imgBlue.getScaledHeight()); PdfImage stream = new PdfImage(imgBlue, "", null); stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789")); PdfIndirectObject ref = writer.addToBody(stream); imgBlue.setDirectReference(ref.getIndirectReference()); document.add(imgBlue); PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); PdfPCell cell = new PdfPCell(new Paragraph(contract.getName(), font18n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); cell = new PdfPCell(new Paragraph("Version: " + contract.getVersion(), font8n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); cell = new PdfPCell(new Paragraph("Review: " + contract.getReview(), font8n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); cell = new PdfPCell(new Paragraph(contract.getRemark(), font12n)); cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(2); cell.setPadding(5); table.addCell(cell); document.add(table); // Start a new page document.newPage(); HTMLWorker htmlWorker = new HTMLWorker(document); htmlWorker.parse(new StringReader(contract.getRemark())); // Start a new page document.newPage(); document.add(new Paragraph("Review Board", font18n)); document.add(new LineSeparator(0.5f, 100, null, 0, -5)); table = new PdfPTable(3); table.setWidthPercentage(100); cell = new PdfPCell(new Paragraph("Review Board", font18n)); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(new Paragraph("Version", font12n)); table.addCell(cell); cell = new PdfPCell(new Paragraph("Date", font12n)); table.addCell(cell); cell = new PdfPCell(new Paragraph("Review", font12n)); table.addCell(cell); for (HistoryContractEntity history : historyContractService.listing(contract)) { cell = new PdfPCell(new Paragraph(history.getVersion().toString(), font8n)); table.addCell(cell); cell = new PdfPCell(new Paragraph(history.getStartdate().toString(), font8n)); table.addCell(cell); cell = new PdfPCell(new Paragraph(history.getReview(), font8n)); table.addCell(cell); } document.add(table); document.close(); FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.reset(); response.addHeader("Content-Type", "application/force-download"); String filename = URLEncoder.encode(contract.getName() + ".pdf", "UTF-8"); // response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename); response.getOutputStream().write(output.toByteArray()); response.getOutputStream().flush(); context.responseComplete(); context.renderResponse(); } catch (BadPdfFormatException | IOException ex) { Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (DocumentException ex) { Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex); } }