List of usage examples for com.lowagie.text Image getInstance
public static Image getInstance(Image image)
From source file:com.conecta.sat.utils.BuildImportTokensPDF.java
@Override protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter writer, HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. System.out.println("Into BuildImportTokensPDF"); hsr1.setContentType("application/pdf"); DateFormat name = new SimpleDateFormat("ddMMyyyyhhmmss"); hsr1.setHeader("Content-disposition", "attachment; filename=ImportTokens" + name.format(new Date()) + ".pdf"); ImportTokensPDF list = (ImportTokensPDF) map.get("list"); String nombreUsuario = (String) map.get("nombreUsuario"); PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100);/* w ww .j a v a 2 s . co m*/ table.setSpacingBefore(10); Font font = FontFactory.getFont(FontFactory.HELVETICA); try { font.setFamily(fontName); } catch (Exception e) { font.setFamily("Verdana"); } try { ServletContext servletContext = hsr.getSession().getServletContext(); String relativeWebPath = logoPath; String absoluteDiskPath = servletContext.getRealPath(relativeWebPath); Image logo = Image.getInstance(absoluteDiskPath); // Image logo = Image.getInstance("logo.png"); System.out.println("La imagen se cargo correctamente"); logo.scaleToFit(widthLogo, heightLogo); document.add(logo); } catch (Exception e) { System.err.println("ERROR" + Excepciones.getStackTrace(e)); document.add(new Paragraph("Sin imagen " + logoPath)); } font.setSize(fontSize + 3); Paragraph titulo = new Paragraph("Reporte de Tokens", font); titulo.setAlignment(Element.ALIGN_RIGHT); document.add(titulo); Chunk CONNECT = new Chunk(new LineSeparator(0.5f, 100, java.awt.Color.BLACK, Element.ALIGN_CENTER, 3.5f)); document.add(CONNECT); DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm a"); font.setSize(fontSize + 2); Paragraph fecha = new Paragraph(df.format(new Date()), font); fecha.setAlignment(Element.ALIGN_RIGHT); document.add(fecha); Paragraph pNombre = new Paragraph(nombreUsuario, font); pNombre.setAlignment(Element.ALIGN_RIGHT); document.add(pNombre); font.setSize(fontSize); font.setColor(java.awt.Color.white); // define table header cell PdfPCell cell = new PdfPCell(); java.awt.Color color = java.awt.Color.LIGHT_GRAY; cell.setBackgroundColor(color); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(5); // write table header cell.setPhrase(new Phrase("Nmero de empleado que importo en token", font)); table.addCell(cell); cell.setPhrase(new Phrase("Fecha y Hora", font)); table.addCell(cell); cell.setPhrase(new Phrase("Tokens importados", font)); table.addCell(cell); // cell.setPhrase(new Phrase("Centro Financiero", font)); // table.addCell(cell); // // cell.setPhrase(new Phrase("Tipo", font)); // table.addCell(cell); // // cell.setPhrase(new Phrase("Cliente nico", font)); // table.addCell(cell); // // cell.setPhrase(new Phrase("Fecha de ultima modificacin", font)); // table.addCell(cell); Font font2 = FontFactory.getFont(FontFactory.HELVETICA); try { font2.setFamily(fontName); } catch (Exception e) { font2.setFamily("Verdana"); } font2.setSize(fontSize); font2.setColor(java.awt.Color.black); // write table row data // for (ImportTokensPDF pdf : list) { // ImportTokensPDF pdf; cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(list.getUsuario(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(df.format(list.getFecha()), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(list.getTokens(), font2)); table.addCell(cell); // cell = new PdfPCell(); // cell.setHorizontalAlignment( Element.ALIGN_CENTER ); // cell.addElement( new Phrase(pdf.getCentro(),font2) ); // table.addCell(cell); // // cell = new PdfPCell(); // cell.setHorizontalAlignment( Element.ALIGN_CENTER ); // cell.addElement( new Phrase(pdf.getTipo(),font2) ); // table.addCell(cell); // // cell = new PdfPCell(); // cell.setHorizontalAlignment( Element.ALIGN_CENTER ); // cell.addElement( new Phrase(pdf.getCliente(),font2) ); // table.addCell(cell); // // cell = new PdfPCell(); // cell.setHorizontalAlignment( Element.ALIGN_CENTER ); // cell.addElement( new Phrase(pdf.getLastUpdate(),font2) ); // table.addCell(cell); // } document.add(table); }
From source file:com.conecta.sat.utils.BuildPDF.java
@Override protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter writer, HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. hsr1.setContentType("application/pdf"); DateFormat name = new SimpleDateFormat("ddMMyyyyhhmmss"); hsr1.setHeader("Content-disposition", "attachment; filename=Reporte" + name.format(new Date()) + ".pdf"); List<PdfDTO> list = (List<PdfDTO>) map.get("list"); String nombreUsuario = (String) map.get("nombreUsuario"); PdfPTable table = new PdfPTable(9); table.setWidthPercentage(100);/* w w w . ja v a 2 s .c o m*/ table.setSpacingBefore(10); Font font = FontFactory.getFont(FontFactory.HELVETICA); try { font.setFamily(fontName); } catch (Exception e) { font.setFamily("Verdana"); } try { ServletContext servletContext = hsr.getSession().getServletContext(); String relativeWebPath = logoPath; String absoluteDiskPath = servletContext.getRealPath(relativeWebPath); Image logo = Image.getInstance(absoluteDiskPath); // Image logo = Image.getInstance("logo.png"); System.out.println("La imagen se cargo correctamente"); logo.scaleToFit(widthLogo, heightLogo); document.add(logo); } catch (Exception e) { System.err.println("ERROR" + Excepciones.getStackTrace(e)); document.add(new Paragraph("Sin imagen " + logoPath)); } font.setSize(fontSize + 3); Paragraph titulo = new Paragraph("Reporte de Tokens", font); titulo.setAlignment(Element.ALIGN_RIGHT); document.add(titulo); Chunk CONNECT = new Chunk(new LineSeparator(0.5f, 100, java.awt.Color.BLACK, Element.ALIGN_CENTER, 3.5f)); document.add(CONNECT); DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm a"); font.setSize(fontSize + 2); Paragraph fecha = new Paragraph(df.format(new Date()), font); fecha.setAlignment(Element.ALIGN_RIGHT); document.add(fecha); Paragraph pNombre = new Paragraph(nombreUsuario, font); pNombre.setAlignment(Element.ALIGN_RIGHT); document.add(pNombre); font.setSize(fontSize); font.setColor(java.awt.Color.white); // define table header cell PdfPCell cell = new PdfPCell(); java.awt.Color color = java.awt.Color.LIGHT_GRAY; cell.setBackgroundColor(color); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setPadding(5); // write table header cell.setPhrase(new Phrase("Serial Token", font)); table.addCell(cell); // cell.setPhrase(new Phrase("Estatus Entrega", font)); cell.setPhrase(new Phrase("Estatus Entrega al Centro Financiero", font)); table.addCell(cell); cell.setPhrase(new Phrase("Estatus Entrega al Cliente", font)); table.addCell(cell); cell.setPhrase(new Phrase("Estatus Activacin", font)); table.addCell(cell); cell.setPhrase(new Phrase("Centro Financiero", font)); table.addCell(cell); cell.setPhrase(new Phrase("Tipo", font)); table.addCell(cell); cell.setPhrase(new Phrase("Cliente nico", font)); table.addCell(cell); cell.setPhrase(new Phrase("Folio Pivotal", font)); table.addCell(cell); // cell.setPhrase(new Phrase("ID nico", font)); // table.addCell(cell); cell.setPhrase(new Phrase("Fecha de ltima modificacin", font)); table.addCell(cell); Font font2 = FontFactory.getFont(FontFactory.HELVETICA); try { font2.setFamily(fontName); } catch (Exception e) { font2.setFamily("Verdana"); } font2.setSize(fontSize); font2.setColor(java.awt.Color.black); // write table row data for (PdfDTO pdf : list) { cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getSerial(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getEntrega(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getEntrega(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getActivacion(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getCentro(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getTipo(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getCliente(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getFolioPivotal(), font2)); table.addCell(cell); // cell = new PdfPCell(); // cell.setHorizontalAlignment( Element.ALIGN_CENTER ); // cell.addElement( new Phrase(pdf.getIdUnico(),font2) ); // table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getLastUpdate(), font2)); table.addCell(cell); } document.add(table); }
From source file:com.dlya.facturews.DlyaPdfExporter2.java
License:Open Source License
/** * *///from w w w. j av a 2 s. co m protected Image getPxImage() { if (pxImage == null) { try { pxImage = Image.getInstance( JRLoader.loadBytesFromResource("net/sf/jasperreports/engine/images/pixel.GIF")); } catch (Exception e) { throw new JRRuntimeException(e); } } return pxImage; }
From source file:com.efficio.fieldbook.service.LabelPrintingServiceImpl.java
License:Open Source License
/** * Gets the cell height./*from ww w. j ava 2 s . c o m*/ * * @param numberOfRowsPerPage the number of rows per page * @param pageSizeId the page size id * @return the cell height */ /* private float getCellHeight(int numberOfRowsPerPage, int pageSizeId){ if(pageSizeId == AppConstants.SIZE_OF_PAPER_A4){ if(numberOfRowsPerPage == 7){ return 108f; //ok }else if(numberOfRowsPerPage == 8){ return 97f;//ok }else if(numberOfRowsPerPage == 10){ return 72.5f; } }else{ if(numberOfRowsPerPage == 7){ return 108f; //ok }else if(numberOfRowsPerPage == 8){ return 98.1f;//ok }else if(numberOfRowsPerPage == 10){ return 72.5f;//ok } } return 0f; } */ /* (non-Javadoc) * @see com.efficio.fieldbook.service.api.LabelPrintingService#generateLabels(com.efficio.fieldbook.web.fieldmap.bean.UserFieldmap) */ @Override public String generatePDFLabels(List<StudyTrialInstanceInfo> trialInstances, UserLabelPrinting userLabelPrinting, ByteArrayOutputStream baos) throws MiddlewareQueryException { // setUserLabelPrinting(form.getUserLabelPrinting()); int pageSizeId = Integer.parseInt(userLabelPrinting.getSizeOfLabelSheet()); int numberOfLabelPerRow = Integer.parseInt(userLabelPrinting.getNumberOfLabelPerRow()); int numberofRowsPerPageOfLabel = Integer.parseInt(userLabelPrinting.getNumberOfRowsPerPageOfLabel()); int totalPerPage = numberOfLabelPerRow * numberofRowsPerPageOfLabel; String leftSelectedFields = userLabelPrinting.getLeftSelectedLabelFields(); String rightSelectedFields = userLabelPrinting.getRightSelectedLabelFields(); String barcodeNeeded = userLabelPrinting.getBarcodeNeeded(); String firstBarcodeField = userLabelPrinting.getFirstBarcodeField(); String secondBarcodeField = userLabelPrinting.getSecondBarcodeField(); String thirdBarcodeField = userLabelPrinting.getThirdBarcodeField(); String currentDate = DateUtil.getCurrentDate(); // String fileName = currentDate + ".pdf"; String fileName = userLabelPrinting.getFilenameDLLocation(); try { FileOutputStream fileOutputStream = new FileOutputStream(fileName); try { // Image image1 = Image.getInstance(imageLocation); // PageSize.A4 LabelPaper paper = LabelPaperFactory.generateLabelPaper(numberOfLabelPerRow, numberofRowsPerPageOfLabel, pageSizeId); Rectangle pageSize = PageSize.LETTER; if (pageSizeId == AppConstants.SIZE_OF_PAPER_A4.getInt()) pageSize = PageSize.A4; Document document = new Document(pageSize); /* * 2, 2, 33.3f, 5 * 15, 0, 42, 5 * 10, 0, 17, 5 * 10, 0, 17, 5 * 5, 0, 0, 5 * 15, 0, 37, 5 * 10, 0, 17, 5 if (pageSizeId == AppConstants.SIZE_OF_PAPER_LETTER) { if (numberofRowsPerPageOfLabel == 7) document.setMargins(10, 0, 17, 5); else if (numberofRowsPerPageOfLabel == 8) document.setMargins(5, 0, 0, 5); else if (numberofRowsPerPageOfLabel == 10) document.setMargins(2, 2, 33.3f, 5); } else if (pageSizeId == AppConstants.SIZE_OF_PAPER_A4) { if (numberofRowsPerPageOfLabel == 7) document.setMargins(15, 0, 42, 5); else if (numberofRowsPerPageOfLabel == 8) document.setMargins(15, 0, 37, 5); else if (numberofRowsPerPageOfLabel == 10) document.setMargins(6, 2, 17.5f, 5); } */ //float marginLeft, float marginRight, float marginTop, float marginBottom document.setMargins(paper.getMarginLeft(), paper.getMarginRight(), paper.getMarginTop(), paper.getMarginBottom()); // PdfWriter writer = PdfWriter.getInstance(document, baos); PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream); // step 3 document.open(); // step 4 PdfContentByte canvas = writer.getDirectContent(); int i = 0; int fixTableRowSize = numberOfLabelPerRow; PdfPTable table = new PdfPTable(fixTableRowSize); // table.writeSelectedRows(0, -1, 10, 12, canvas); float columnWidthSize = 265f;// 180f; float[] widthColumns = new float[fixTableRowSize]; for (int counter = 0; counter < widthColumns.length; counter++) { widthColumns[counter] = columnWidthSize; } table.setWidths(widthColumns); table.setWidthPercentage(100); int width = 600; int height = 75; List<File> filesToBeDeleted = new ArrayList<File>(); //float cellHeight = getCellHeight(numberofRowsPerPageOfLabel, pageSizeId); float cellHeight = paper.getCellHeight(); for (StudyTrialInstanceInfo trialInstance : trialInstances) { FieldMapTrialInstanceInfo fieldMapTrialInstanceInfo = trialInstance.getTrialInstance(); Map<String, String> moreFieldInfo = new HashMap<String, String>(); moreFieldInfo.put("locationName", fieldMapTrialInstanceInfo.getLocationName()); moreFieldInfo.put("blockName", fieldMapTrialInstanceInfo.getBlockName()); moreFieldInfo.put("selectedName", trialInstance.getFieldbookName()); moreFieldInfo.put("trialInstanceNumber", fieldMapTrialInstanceInfo.getTrialInstanceNo()); for (FieldMapLabel fieldMapLabel : fieldMapTrialInstanceInfo.getFieldMapLabels()) { i++; String barcodeLabel = generateBarcodeField(moreFieldInfo, fieldMapLabel, firstBarcodeField, secondBarcodeField, thirdBarcodeField, barcodeNeeded); if ("0".equalsIgnoreCase(barcodeNeeded)) { barcodeLabel = " "; } BitMatrix bitMatrix = new Code128Writer().encode(barcodeLabel, BarcodeFormat.CODE_128, width, height, null); String imageLocation = System.getProperty("user.home") + "/" + Math.random() + ".png"; File imageFile = new File(imageLocation); FileOutputStream fout = new FileOutputStream(imageFile); MatrixToImageWriter.writeToStream(bitMatrix, "png", fout); filesToBeDeleted.add(imageFile); Image mainImage = Image.getInstance(imageLocation); PdfPCell cell = new PdfPCell(); cell.setFixedHeight(cellHeight); cell.setNoWrap(false); cell.setPadding(5f); cell.setPaddingBottom(1f); PdfPTable innerImageTableInfo = new PdfPTable(1); innerImageTableInfo.setWidths(new float[] { 1 }); innerImageTableInfo.setWidthPercentage(82); PdfPCell cellImage = new PdfPCell(); if ("1".equalsIgnoreCase(barcodeNeeded)) { cellImage.addElement(mainImage); } else { cellImage.addElement(new Paragraph(" ")); } cellImage.setBorder(Rectangle.NO_BORDER); cellImage.setBackgroundColor(Color.white); cellImage.setPadding(1.5f); innerImageTableInfo.addCell(cellImage); //float fontSize = 6.8f; float fontSize = paper.getFontSize(); //if (numberofRowsPerPageOfLabel == 10) // fontSize = 4.8f; Font fontNormal = FontFactory.getFont("Arial", fontSize, Font.NORMAL); // cell.addElement(mainImage); cell.addElement(innerImageTableInfo); cell.addElement(new Paragraph()); for (int row = 0; row < 5; row++) { if (row == 0) { PdfPTable innerDataTableInfo = new PdfPTable(1); innerDataTableInfo.setWidths(new float[] { 1 }); innerDataTableInfo.setWidthPercentage(85); Font fontNormalData = FontFactory.getFont("Arial", 5.0f, Font.NORMAL); PdfPCell cellInnerData = new PdfPCell(new Phrase(barcodeLabel, fontNormalData)); cellInnerData.setBorder(Rectangle.NO_BORDER); cellInnerData.setBackgroundColor(Color.white); cellInnerData.setPaddingBottom(0.2f); cellInnerData.setPaddingTop(0.2f); cellInnerData.setHorizontalAlignment(Element.ALIGN_MIDDLE); innerDataTableInfo.addCell(cellInnerData); innerDataTableInfo.setHorizontalAlignment(Element.ALIGN_MIDDLE); cell.addElement(innerDataTableInfo); } PdfPTable innerTableInfo = new PdfPTable(2); innerTableInfo.setWidths(new float[] { 1, 1 }); innerTableInfo.setWidthPercentage(85); String leftText = generateBarcodeLabel(moreFieldInfo, fieldMapLabel, leftSelectedFields, row); PdfPCell cellInnerLeft = new PdfPCell(new Paragraph(leftText, fontNormal)); cellInnerLeft.setBorder(Rectangle.NO_BORDER); cellInnerLeft.setBackgroundColor(Color.white); cellInnerLeft.setPaddingBottom(0.5f); cellInnerLeft.setPaddingTop(0.5f); innerTableInfo.addCell(cellInnerLeft); String rightText = generateBarcodeLabel(moreFieldInfo, fieldMapLabel, rightSelectedFields, row); PdfPCell cellInnerRight = new PdfPCell(new Paragraph(rightText, fontNormal)); cellInnerRight.setBorder(Rectangle.NO_BORDER); cellInnerRight.setBackgroundColor(Color.white); cellInnerRight.setPaddingBottom(0.5f); cellInnerRight.setPaddingTop(0.5f); innerTableInfo.addCell(cellInnerRight); cell.addElement(innerTableInfo); } cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(Color.white); //cell.setBorderColor(Color.BLUE); table.addCell(cell); if (i % numberOfLabelPerRow == 0) { // we go the next line int needed = fixTableRowSize - numberOfLabelPerRow; for (int neededCount = 0; neededCount < needed; neededCount++) { PdfPCell cellNeeded = new PdfPCell(); cellNeeded.setBorder(Rectangle.NO_BORDER); cellNeeded.setBackgroundColor(Color.white); table.addCell(cellNeeded); } table.completeRow(); if (numberofRowsPerPageOfLabel == 10) { //table.setSpacingAfter(9f); table.setSpacingAfter(paper.getSpacingAfter()); } document.add(table); table = new PdfPTable(fixTableRowSize); table.setWidths(widthColumns); table.setWidthPercentage(100); } if (i % totalPerPage == 0) { // we go the next page document.newPage(); } fout.flush(); fout.close(); } } // we need to add the last row if (i % numberOfLabelPerRow != 0) { // we go the next line int needed = fixTableRowSize - numberOfLabelPerRow; int remaining = numberOfLabelPerRow - (i % numberOfLabelPerRow); for (int neededCount = 0; neededCount < remaining; neededCount++) { PdfPCell cellNeeded = new PdfPCell(); cellNeeded.setBorder(Rectangle.NO_BORDER); cellNeeded.setBackgroundColor(Color.white); table.addCell(cellNeeded); } table.completeRow(); if (numberofRowsPerPageOfLabel == 10) { table.setSpacingAfter(paper.getSpacingAfter()); } document.add(table); table = new PdfPTable(fixTableRowSize); table.setWidths(widthColumns); table.setWidthPercentage(100); } document.close(); for (File file : filesToBeDeleted) { file.delete(); } fileOutputStream.close(); } catch (FileNotFoundException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } } catch (WriterException e) { LOG.error(e.getMessage(), e); } catch (Exception e) { LOG.error(e.getMessage(), e); } return fileName; }
From source file:com.efficio.fieldbook.web.demo.controller.BarCodeController.java
License:Open Source License
/** * Upload file./*from w w w . j a v a 2 s . c o m*/ * * @param uploadForm the upload form * @param result the result * @param model the model * @return the string */ @ResponseBody @RequestMapping(method = RequestMethod.POST) public String uploadFile(@ModelAttribute("barCodeForm") BarCodeForm barCodeForm, BindingResult result, Model model, HttpServletResponse response) { int width = 60; int height = 48; String delimeter = "|"; StringBuilder barCodeString = new StringBuilder(); barCodeString.append(barCodeForm.getEntryNo()).append(delimeter).append(barCodeForm.getGid()) .append(delimeter).append(barCodeForm.getDesignation()).append(delimeter) .append(barCodeForm.getName()).append(delimeter).append(barCodeForm.getRep()).append(delimeter) .append(barCodeForm.getColumn()).append(delimeter).append(barCodeForm.getRange()); BitMatrix bitMatrix; try { String barCodeLabel = "3|SM114-1A-1-1-1B"; //barCodeLabel = barCodeString.toString(); bitMatrix = new Code128Writer().encode(barCodeLabel, BarcodeFormat.CODE_128, width, height, null); String imageLocation = Math.random() + ".png"; //"src/test/resources/barcode/zxing_barcode.png"; MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File(imageLocation))); String currentDate = DateUtil.getCurrentDate(); String fileName = currentDate + ".doc"; response.setHeader("Content-disposition", "attachment; filename=" + fileName); File xls = new File(fileName); // the selected name + current date FileInputStream in; try { Image image1 = Image.getInstance(imageLocation); //image1.scalePercent(50); //image1.setAbsolutePosition(10f, 10f); //document.add(image1); //PageSize.A4 Document document = new Document(PageSize.LETTER); // step 2 PdfWriter.getInstance(document, new FileOutputStream(fileName)); // step 3 document.open(); // step 4 // we'll use 4 images in this example /* Image[] img = { Image.getInstance(String.format(RESOURCE, "0120903")), Image.getInstance(String.format(RESOURCE, "0290334")), Image.getInstance(String.format(RESOURCE, "0376994")), Image.getInstance(String.format(RESOURCE, "0348150")) };*/ PdfPTable table = new PdfPTable(4); table.setWidthPercentage(100); // first movie table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM); Phrase phrase = new Phrase(); phrase.add("Entry # " + barCodeForm.getEntryNo()); phrase.add("\n"); phrase.add("Designation " + barCodeForm.getDesignation()); phrase.add("\n"); phrase.add("Rep " + barCodeForm.getRep()); phrase.add("\n"); phrase.add("GID " + barCodeForm.getGid()); phrase.add("\n"); phrase.add("Trial/Nursery Name " + barCodeForm.getName()); phrase.add("\n"); phrase.add("Col " + barCodeForm.getColumn()); phrase.add("\n"); phrase.add("Range " + barCodeForm.getRange()); table.addCell(phrase); // we add the image with addCell() table.addCell(image1); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM); table.addCell("X-Men: The Last Stand"); // we add the image with addCell() table.addCell(image1); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM); table.addCell("X-Men: The Last Stand"); // we add the image with addCell() table.addCell(image1); // we complete the table (otherwise the last row won't be rendered) table.completeRow(); document.add(table); document.close(); in = new FileInputStream(xls); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; // use bigger if you want int length = 0; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); } catch (FileNotFoundException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } return ""; } catch (WriterException e) { LOG.error(e.getMessage(), e); } catch (Exception e) { LOG.error(e.getMessage(), e); } return show(barCodeForm, model); }
From source file:com.estate.pdf.Page.java
protected Image loadImage(String name) { Image img = null;/*from w w w . j a v a 2 s . c om*/ try { img = Image.getInstance(Locations.getImageLocation() + name); } catch (Exception e) { System.out.println(e.getMessage()); } return img; }
From source file:com.estate.pdf.PageBorder.java
public void draw(Document doc, int iconNum, String pageNum, String toolName) { try {// w ww . j av a2 s .c om PdfContentByte cb = writer.getDirectContentUnder(); BaseFont fontBold = BaseFont.createFont(Locations.getFontLocation() + "timesbd.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252, BaseFont.EMBEDDED); Rectangle rct = new Rectangle(doc.getPageSize()); float iconBase = (1.25f * 72); // This is the base of the icons on // the page Image icon = Image.getInstance(Locations.getImageLocation() + icons[iconNum]); // Image box = Image.getInstance(Locations.ImageLocation() + // "blueBOX.png"); icon.scalePercent(23); float scale = .23f; float iconLeft = (icon.getWidth() / 2) * scale; float boxSize = (.1875f * 72); // Adjust the top rct.setTop(rct.getTop() - (.5f * 72)); // Place the Icon icon.setAbsolutePosition((1.25f * 72) - iconLeft, rct.getTop() - iconBase); doc.add(icon); // Set our line color cb.setRGBColorStroke(0, 72, 117); cb.setLineWidth(.75f); // do the bottom line cb.moveTo(1.25f * 72, (.5f * 72)); cb.lineTo(rct.getRight() - (.5f * 72), (.5f * 72)); cb.moveTo(1.25f * 72, (.5f * 72)); cb.lineTo(1.25f * 72, rct.getTop()); // stroke the lines cb.stroke(); // Do the lower left box cb.rectangle((1.25f - 0.09375f) * 72, (.5f - 0.09375f) * 72, boxSize, boxSize); cb.setRGBColorFill(0, 72, 117); cb.closePathFillStroke(); // Do the lower right box Rectangle pnRect = new Rectangle(0, 0); pnRect.setLeft(rct.getRight() - ((.5f + 0.09375f) * 72)); pnRect.setTop((.5f - 0.09375f) * 72); pnRect.setRight(pnRect.getLeft() + boxSize); pnRect.setBottom(pnRect.getTop() - boxSize); // cb.rectangle(rct.getRight() - ((.5f + 0.09375f) * 72), (.5f - // 0.09375f) * 72, (.1875f * 72), (.1875f * 72)); cb.rectangle(pnRect.getLeft(), pnRect.getTop(), boxSize, boxSize); cb.setRGBColorFill(0, 72, 117); cb.closePathFillStroke(); // Now we do the page number if one is supplied. if (pageNum.length() > 0) { float pnHeight = fontBold.getAscentPoint(pageNum, 9) - fontBold.getDescentPoint(pageNum, 9); float pnWidth = fontBold.getWidthPoint(pageNum, 9); float l = pnRect.getLeft() + ((boxSize - pnWidth) / 2); float b = pnRect.getTop() + ((boxSize - pnHeight) / 2); cb.beginText(); cb.setFontAndSize(fontBold, 9); cb.setRGBColorFill(255, 255, 255); cb.setTextMatrix(l, b); cb.showText(pageNum); cb.endText(); } // Display the copyright SimpleDateFormat df = new SimpleDateFormat("yyyy"); char cs = 0x00a9; // Unicode for the copyright symbol String copyRight = com.estate.constants.StringConstants.copyRight + cs + " " + df.format(new Date()); float crWidth; float crLeft; crWidth = font.getWidthPoint(copyRight, 8); cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); crLeft = (doc.getPageSize().getRight() - crWidth) / 2; cb.setTextMatrix(crLeft, .375f * 72); // Place the base of the // copyright at 3/8" up from // the bottom cb.showText(copyRight); cb.endText(); if (toolName.length() > 0) { cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); cb.setTextMatrix((1.25f * 72) + boxSize, .375f * 72); cb.showText(toolName); cb.endText(); } // Fix a licensee at left cb.beginText(); cb.setFontAndSize(font, 8); cb.setRGBColorFill(0, 0, 0); cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, " Licensee: " + getLicense(), (10.2f * 72), .375f * 72, 0); cb.endText(); } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:com.estate.report.Scenario.java
public Rectangle placeHeading() { Rectangle rect = new Rectangle(document.getPageSize()); BaseFont font = PageUtils.LoadFont("GARA.TTF"); rect.setBottom(rect.getTop() - (72 * .6f)); HeadingText ht = new HeadingText(this.writer); rect.setBottom(rect.getBottom() - (72 * .33f)); ht.setColor(57, 57, 57);// w w w .j a v a2 s. com ht.setCapsColor(98, 98, 98); if (sc1) ht.display(rect, 12f, font, 3f, heading, HeadingText.DHT_CENTER); else ht.display(rect, 12f, font, 3f, heading2, HeadingText.DHT_CENTER); rect.setTop(rect.getBottom() - _1_8TH); rect.setBottom(prctFull.getBottom()); rect.setLeft(_1_2TH); rect.setRight(rect.getLeft() + (11.f * 72)); PageBorder pb = new PageBorder(writer); pb.setLicense(userInfo.getPlannerFirstName() + " " + userInfo.getPlannerLastName()); pb.drawNoBorder(document, Integer.toString(pageNum)); try { Rectangle rct = new Rectangle(document.getPageSize()); float iconBase = (.75f * 72); // This is the base of the icons on // the page Image icon = Image.getInstance(Locations.getImageLocation() + "pawn.png"); // Image box = Image.getInstance(Locations.ImageLocation() + // "blueBOX.png"); icon.scalePercent(23); float scale = .23f; float iconLeft = (icon.getWidth() / 2) * scale; // Adjust the top rct.setTop(rct.getTop()); // Place the Icon icon.setAbsolutePosition((.5f * 72) - iconLeft, rct.getTop() - iconBase); document.add(icon); } catch (Exception e) { System.err.println(e.getMessage()); } drawLifeExp(); return rect; }
From source file:com.eugenefe.controller.TableBean.java
License:Apache License
public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException { Document pdf = (Document) document; pdf.open();/*from w ww . j a va 2 s . co m*/ pdf.setPageSize(PageSize.A4); ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); String logo = servletContext.getRealPath("") + File.separator + "images" + File.separator + "prime_logo.png"; pdf.add(Image.getInstance(logo)); }
From source file:com.ev.export.AnnualPDFExporter.java
License:Apache License
private void addDiagram() throws Exception { if (chart != null) { File imgFile = new File(System.currentTimeMillis() + JPEG_SUFFIX); ChartUtilities.saveChartAsJPEG(imgFile, chart, 500, 300); Element elem = Image.getInstance(imgFile.getCanonicalPath()); document.add(elem);/* ww w . java 2s . c om*/ imgFile.delete(); } else { throw new ExporterException("Could not find diagram."); } }