List of usage examples for com.lowagie.text Document right
public float right()
From source file:com.preparatic.archivos.PdfGenerator.java
License:Apache License
@Override public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); // Phrase header = new Phrase("this is a header", ffont); // ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, // header, // (document.right() - document.left()) / 2 + document.leftMargin(), // document.top() + 10, 0); Phrase footer = new Phrase(String.format(title + ", pgina %d", pageNumber), ffont); ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer, (document.right() - document.left()) / 2 + document.leftMargin(), document.bottom() - 10, 0); }
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
private void buildFooter(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();//from w w w . j a va2s. co m String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " "; float textBase = document.bottom() - 25; float textSize = FontUtils.getDejavu().getWidthPoint(text, 7); cb.setColorFill(ColorUtils.getLightColor()); cb.setColorStroke(ColorUtils.getLightColor()); cb.setLineWidth(1); cb.setLineDash(2, 2, 1); cb.moveTo(document.left(), document.bottom() - 10); cb.lineTo(document.right(), document.bottom() - 10); cb.stroke(); cb.beginText(); cb.setFontAndSize(FontUtils.getDejavu(), 7); float adjust = FontUtils.getDejavu().getWidthPoint("0", 7); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); textSize = FontUtils.getDejavu().getWidthPoint(footer.getGeneratedBy(), 7); cb.setTextMatrix(document.right() - textSize, textBase - 10); cb.showText(footer.getGeneratedBy()); textSize = FontUtils.getDejavu().getWidthPoint(generationDate, 7); cb.setTextMatrix(document.right() - textSize, textBase - 20); cb.showText(generationDate); cb.endText(); try { textSize = FontUtils.getDejavu().getWidthPoint(footer.getAdditionalText(), 7); ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(new Phrase(footer.getAdditionalText(), FontUtils.getDejavuRegular7Light()), document.left() + 240, textBase + 10, document.left() + 390, textBase - 25, 10, Element.ALIGN_LEFT); ct.go(); } catch (DocumentException e) { LOG.warn("Problem with additional text generation in report footer."); } try { ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(document.left(), textBase + 10, document.left() + 230, textBase - 25, 10, Element.ALIGN_LEFT); ct.addText(new Phrase(footer.getCompanyName() + "\n", FontUtils.getDejavuRegular7Light())); if (!"".equals(footer.getAddress())) { ct.addText(new Phrase(footer.getAddress() + "\n", FontUtils.getDejavuRegular7Light())); } if (!"".equals(footer.getPhoneEmail())) { ct.addText(new Phrase(footer.getPhoneEmail(), FontUtils.getDejavuRegular7Light())); } ct.go(); } catch (DocumentException e) { LOG.warn("Problem with company text generation in report footer."); } cb.addTemplate(total, document.right() - adjust, textBase); cb.restoreState(); }
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
private void buildHeader(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();//from w w w. ja v a 2s. c om String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " "; float textBase = document.top() + 22; float textSize = FontUtils.getDejavu().getWidthPoint(text, 7); cb.setColorFill(ColorUtils.getLightColor()); cb.setColorStroke(ColorUtils.getLightColor()); cb.beginText(); cb.setFontAndSize(FontUtils.getDejavu(), 7); float adjust = FontUtils.getDejavu().getWidthPoint("0", 7); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(total, document.right() - adjust, textBase); cb.setLineWidth(1); cb.setLineDash(2, 2, 1); cb.moveTo(document.left(), document.top() + 12); cb.lineTo(document.right(), document.top() + 12); cb.stroke(); cb.restoreState(); }
From source file:com.tsp.gespro.bo.DegustacionBO.java
/** * Representacin impresa PDF/*from ww w .ja v a 2 s. c o m*/ */ public ByteArrayOutputStream toPdf(UsuarioBO user) throws Exception { SimpleDateFormat fecha = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat hora = new SimpleDateFormat("HH:mm:ss"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfITextUtil obj = new PdfITextUtil(); //Tamao de documento (Tamao Carta) com.lowagie.text.Document doc = new com.lowagie.text.Document(PageSize.LETTER); //Definicin de Fuentes a usar Font letraOcho = new Font(Font.HELVETICA, 8, Font.NORMAL); Font letraOchoBold = new Font(Font.HELVETICA, 8, Font.BOLD); Font letraNueve = new Font(Font.HELVETICA, 9, Font.NORMAL); Font letraNueveBold = new Font(Font.HELVETICA, 9, Font.BOLD); Font letraNueveBoldRojo = new Font(Font.TIMES_ROMAN, 9, Font.BOLD, Color.red); Font letraNueveBoldAzul = new Font(Font.TIMES_ROMAN, 9, Font.BOLD, Color.blue); Font letraOchoVerde = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL, Color.green); Font letra14Bold = new Font(Font.HELVETICA, 14, Font.BOLD); String msgError = ""; File fileImageLogo = null; try { if (user != null) fileImageLogo = new ImagenPersonalBO(this.conn) .getFileImagenPersonalByEmpresa(user.getUser().getIdEmpresa()); } catch (Exception ex) { ex.printStackTrace(); } try { //Preparamos writer de PDF PdfWriter writer = PdfWriter.getInstance(doc, baos); EventPDF eventPDF = new EventPDF(doc, user, ReportBO.DEGUSTACION_REPRESENTACION_IMPRESA, fileImageLogo); writer.setPageEvent(eventPDF); //Ajustamos margenes de pgina //doc.setMargins(50, 50, 120, 50); //doc.setMargins(20, 20, 80, 20); doc.setMargins(10, 10, 150, 40); //Iniciamos documento doc.open(); //Creamos tabla principal PdfPTable mainTable = new PdfPTable(1); mainTable.setTotalWidth(550); mainTable.setLockedWidth(true); //CONTENIDO ------------- //SALTO DE L?NEA obj.agregaCelda(mainTable, letraOcho, " ", new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); //Cabecera (Datos Generales)--------------------- PdfPTable tAux = new PdfPTable(1); Cliente clienteDto = null; DatosUsuario datosUsuarioVendedor = null; try { if (this.degustacion.getIdCliente() > 0) clienteDto = new ClienteBO(this.degustacion.getIdCliente(), this.conn).getCliente(); if (this.degustacion.getIdUsuario() > 0) datosUsuarioVendedor = new UsuarioBO(this.degustacion.getIdUsuario()).getDatosUsuario(); } catch (Exception ex) { ex.printStackTrace(); } //Datos informativos generales PdfPTable tInfoGeneral = new PdfPTable(1); tInfoGeneral.setTotalWidth(160); tInfoGeneral.setLockedWidth(true); obj.agregaCelda(tInfoGeneral, letraNueveBold, Color.lightGray, "ID Degustacin", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(tInfoGeneral, letraNueveBoldRojo, "" + this.degustacion.getIdDegustacion(), new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(tInfoGeneral, letraNueveBold, Color.lightGray, "Fecha Degustacin", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(tInfoGeneral, letraNueve, DateManage.dateToStringEspanol(this.degustacion.getFechaApertura()), new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(tInfoGeneral, letraNueveBold, Color.lightGray, "Promotor", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(tInfoGeneral, letraNueve, datosUsuarioVendedor != null ? (datosUsuarioVendedor.getNombre() + " " + datosUsuarioVendedor.getApellidoPat()) : "Sin vendedor asignado", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); //Pintamos datos informativos Generales en la esquina superior derecha PdfContentByte cb = writer.getDirectContent(); tInfoGeneral.writeSelectedRows(0, -1, doc.right() - 180, doc.top() + 100, cb); //Datos de Cliente/Prospecto PdfPTable tCliente = new PdfPTable(2); tCliente.setTotalWidth(550); tCliente.setLockedWidth(true); obj.agregaCelda(tCliente, letraNueveBold, Color.lightGray, "Cliente", new boolean[] { false, false, false, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 2); tAux = new PdfPTable(1); tAux.setTotalWidth(275); tAux.setLockedWidth(true); try { obj.agregaCelda(tAux, letraOcho, "Nombre Comercial: " + (clienteDto != null ? clienteDto.getNombreComercial() : ""), new boolean[] { false, true, false, false }, Element.ALIGN_LEFT, Element.ALIGN_TOP, 0, new int[0], 1); } catch (Exception ex) { ex.printStackTrace(); } obj.agregaTabla(tCliente, tAux, new boolean[] { false, false, false, false }, Element.ALIGN_LEFT, Element.ALIGN_TOP, 0, new int[0], 1); obj.agregaCelda(tCliente, letraOcho, "" + "DOMICILIO: \n" + (clienteDto != null ? "Calle: " + clienteDto.getCalle() : "") + " " + (clienteDto != null ? clienteDto.getNumero() : "") + " " + (clienteDto != null ? clienteDto.getNumeroInterior() : "") + (clienteDto != null ? " Col: " + clienteDto.getColonia() : "") + (clienteDto != null ? " \nDeleg./Municipio: " + clienteDto.getMunicipio() : "") + (clienteDto != null ? " Estado: " + clienteDto.getEstado() : "") + (clienteDto != null ? " \nC.P. " + clienteDto.getCodigoPostal() : ""), new boolean[] { false, true, false, true }, Element.ALIGN_LEFT, Element.ALIGN_TOP, 0, new int[0], 1); obj.agregaTabla(mainTable, tCliente, new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_TOP, 0, new int[0], 1); //FIN Cabecera (Datos Generales)----------------- //DOBLE SALTO DE L?NEA obj.agregaCelda(mainTable, letraOcho, null, " ", new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(mainTable, letraOcho, null, " ", new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); int colsDetalles = 6; PdfPTable tDetalles = new PdfPTable(colsDetalles);//6); tDetalles.setTotalWidth(550); tDetalles.setWidths(new int[] { 200, 70, 70, 70, 70, 70 }); tDetalles.setLockedWidth(true); /*CABECERA*/ obj.agregaCelda(tDetalles, letraNueveBold, Color.lightGray, "Producto Degustado", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tDetalles, letraNueveBold, Color.lightGray, "Hr Inicio", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tDetalles, letraNueveBold, Color.lightGray, "Hr Termino", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tDetalles, letraNueveBold, Color.lightGray, "Inventario Inicial", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tDetalles, letraNueveBold, Color.lightGray, "Inventario Final", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tDetalles, letraNueveBold, Color.lightGray, "Piezas Degustadas", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); /*FIN DE CABECERA*/ Degustacion[] degustacionDtos = new Degustacion[0]; try { degustacionDtos = this.findDegustaciones(this.degustacion.getIdDegustacion(), this.degustacion.getIdEmpresa(), -1, -1, ""); } catch (Exception e) { } //Listado de Productos for (Degustacion item : degustacionDtos) { if (item != null) { Concepto conceptoDto = null; try { ConceptoBO conceptoBO = new ConceptoBO(item.getIdConcepto(), this.conn); conceptoDto = conceptoBO.getConcepto(); } catch (Exception e) { } //Producto obj.agregaCelda(tDetalles, letraOcho, null, conceptoDto.getNombreDesencriptado(), new boolean[] { true, true, true, true }, Element.ALIGN_JUSTIFIED, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //Inicio obj.agregaCelda(tDetalles, letraOcho, null, hora.format(item.getFechaApertura()), new boolean[] { true, true, true, true }, Element.ALIGN_JUSTIFIED, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //Termino obj.agregaCelda(tDetalles, letraOcho, null, hora.format(item.getFechaCierre()), new boolean[] { true, true, true, true }, Element.ALIGN_RIGHT, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //Inv inicial obj.agregaCelda(tDetalles, letraOcho, null, "" + (item.getCantidad()), new boolean[] { true, true, true, true }, Element.ALIGN_RIGHT, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //inv final obj.agregaCelda(tDetalles, letraOcho, null, "" + (item.getCantidadCierre()), new boolean[] { true, true, true, true }, Element.ALIGN_RIGHT, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //Piezas obj.agregaCelda(tDetalles, letraOcho, null, "" + (item.getCantidad() - item.getCantidadCierre()), new boolean[] { true, true, true, true }, Element.ALIGN_RIGHT, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); } } obj.agregaTabla(mainTable, tDetalles, new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_TOP, 0, new int[0], 1); //DOBLE SALTO DE L?NEA obj.agregaCelda(mainTable, letraOcho, null, " ", new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(mainTable, letraOcho, null, " ", new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); /*prods Vendidos durante degustacion*/ PdfPTable tVenta = new PdfPTable(4); tVenta.setTotalWidth(550); tVenta.setWidths(new int[] { 70, 140, 170, 120 }); tVenta.setLockedWidth(true); obj.agregaCelda(tVenta, letraNueveBold, Color.lightGray, "Producto Vendido durante Degutacin", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 4); Productosvendidos[] pedidoProductoDto = new Productosvendidos[0]; String filtroBusqueda = " ID_PEDIDO IN ( SELECT ID_PEDIDO FROM sgfens_pedido WHERE ID_EMPRESA = '" + this.degustacion.getIdEmpresa() + "' AND ID_TIPO_PEDIDO = 2 AND ID_USUARIO_VENDEDOR = " + this.degustacion.getIdUsuario() + " AND " + " ID_ESTATUS_PEDIDO<>2 AND FECHA_PEDIDO BETWEEN '" + this.degustacion.getFechaApertura() + "' AND '" + this.degustacion.getFechaCierre() + "') "; try { pedidoProductoDto = new ProductosvendidosDaoImpl().findByDynamicWhere(filtroBusqueda, null); } catch (Exception e) { } if (pedidoProductoDto.length > 0) { obj.agregaCelda(tVenta, letraOchoBold, null, "Cdigo", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tVenta, letraOchoBold, null, "Nombre", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tVenta, letraOchoBold, null, "Descripcin", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); obj.agregaCelda(tVenta, letraOchoBold, null, "Unidades Vendidas", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 1); for (Productosvendidos pedidoProd : pedidoProductoDto) { if (pedidoProd != null) { Concepto conceptoDto = null; try { ConceptoBO conceptoBO = new ConceptoBO(pedidoProd.getIdConcepto(), this.conn); conceptoDto = conceptoBO.getConcepto(); } catch (Exception e) { } //Cdigo obj.agregaCelda(tVenta, letraOcho, null, conceptoDto.getIdentificacion(), new boolean[] { true, true, true, true }, Element.ALIGN_JUSTIFIED, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //Nombre obj.agregaCelda(tVenta, letraOcho, null, conceptoDto.getNombreDesencriptado(), new boolean[] { true, true, true, true }, Element.ALIGN_JUSTIFIED, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //Descripcin obj.agregaCelda(tVenta, letraOcho, null, conceptoDto.getDescripcion(), new boolean[] { true, true, true, true }, Element.ALIGN_RIGHT, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); //Unidades Vendidas obj.agregaCelda(tVenta, letraOcho, null, "" + (pedidoProd.getCantidad()), new boolean[] { true, true, true, true }, Element.ALIGN_RIGHT, Element.ALIGN_TOP, 15, new int[] { 5, 5, 5, 5 }, 1); } } } else { obj.agregaCelda(tVenta, letraOcho, null, "No se registrarn datos.", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 4); } obj.agregaTabla(mainTable, tVenta, new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_TOP, 0, new int[0], 1); //DOBLE SALTO DE L?NEA obj.agregaCelda(mainTable, letraOcho, null, " ", new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); obj.agregaCelda(mainTable, letraOcho, null, " ", new boolean[] { false, false, false, false }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 0, new int[0], 1); /*Comentarios Adicionales*/ PdfPTable tComenst = new PdfPTable(1); tComenst.setTotalWidth(550); tComenst.setWidths(new int[] { 550 }); tComenst.setLockedWidth(true); obj.agregaCelda(tComenst, letraNueveBold, Color.lightGray, "Comentarios", new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 2); obj.agregaCelda(tComenst, letraOcho, null, this.degustacion.getComentariosCierre(), new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 15, new int[] { 5, 5, 5, 5 }, 2); obj.agregaTabla(mainTable, tComenst, new boolean[] { true, true, true, true }, Element.ALIGN_CENTER, Element.ALIGN_TOP, 0, new int[0], 1); //FIN DE CONTENIDO -------- //Aadimos tabla principal construida al documento doc.add(mainTable); mainTable.flushContent(); } catch (Exception ex) { msgError = "No se ha podido generar la representacin impresa de la Degustacion en formato PDF:<br/>" + ex.toString(); } finally { if (doc.isOpen()) doc.close(); } if (!msgError.equals("")) { throw new Exception(msgError); } return baos; }
From source file:de.tr1.cooperator.manager.web.CreateSubscriberListAction.java
License:Open Source License
/** * This method is called by the struts-framework if the submit-button is pressed. * It creates a PDF-File and puts in into the output-stream of the response. *///from w w w . j ava 2 s .co m public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CreateSubscriberListForm myForm = (CreateSubscriberListForm) form; boolean bSortByName; if (myForm.getSortBy().equals(myForm.SORTBYPN)) bSortByName = false; else bSortByName = true; //create Collection for the results... Event eEvent = EventManager.getInstance().getEventByID(myForm.getEventID()); Collection cSubscriberList = UserManager.getInstance().getUsersByCollection(eEvent.getSubscriberList()); Collection cExamResults = EventResultManager.getInstance().getResults(eEvent.getID()); Iterator cSubscriberListIT = cSubscriberList.iterator(); Collection cSubscriberResultList = new ArrayList(); while (cSubscriberListIT.hasNext()) { User CurUser = (User) cSubscriberListIT.next(); String UserPNR = CurUser.getPersonalNumber(); Iterator cExamResultsIT = cExamResults.iterator(); ExamResult curExamResult = null; String ResultUserPNR = null; while (cExamResultsIT.hasNext()) { curExamResult = (ExamResult) cExamResultsIT.next(); ResultUserPNR = curExamResult.getUserPersonalNumber(); if (UserPNR.equals(ResultUserPNR)) break; } if (UserPNR.equals(ResultUserPNR)) { if (bSortByName) { UserResultSortByName URS = new UserResultSortByName(CurUser, "" + curExamResult.getResult()); cSubscriberResultList.add(URS); } else { UserResultSortByPersonalNumber URS = new UserResultSortByPersonalNumber(CurUser, "" + curExamResult.getResult()); cSubscriberResultList.add(URS); } } else { if (bSortByName) { UserResultSortByName URS = new UserResultSortByName(CurUser, "-"); cSubscriberResultList.add(URS); } else { UserResultSortByPersonalNumber URS = new UserResultSortByPersonalNumber(CurUser, "-"); cSubscriberResultList.add(URS); } } } //sort List Collections.sort((List) cSubscriberResultList); BaseFont bf; //36pt = 0.5inch Document document = new Document(PageSize.A4, 36, 36, 72, 72); try { bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); } catch (Exception e) { Log.addLog("CreateSubscriberListAction: Error creating BaseFont: " + e); //2do: add ErrorMessage and return to inputFormular! return mapping.findForward("GeneralFailure"); } //calculate the number of cols and their width int cols = 0; ArrayList alWidth = new ArrayList(); float boldItalicFactor = 1.2f; if (myForm.getShowNumber()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_NUMBER, TABLEHEADER_FONTSIZE))); if (myForm.getShowPersonalNumber()) alWidth.add(new Float( boldItalicFactor * bf.getWidthPoint(TABLEHEADER_PERSONALNUMBER, TABLEHEADER_FONTSIZE))); if (myForm.getShowName()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_NAME, TABLEHEADER_FONTSIZE))); if (myForm.getShowEmail()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_EMAIL, TABLEHEADER_FONTSIZE))); if (myForm.getShowResult()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_RESULT, TABLEHEADER_FONTSIZE))); if (myForm.getAddInfoField()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_INFO, TABLEHEADER_FONTSIZE))); if (myForm.getAddSignField()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_SIGN, TABLEHEADER_FONTSIZE))); cols = alWidth.size(); float totalWidth = 0; //calculate the whole length Iterator alIterator = alWidth.iterator(); for (; alIterator.hasNext(); totalWidth += ((Float) alIterator.next()).floatValue()) ; //calculate relativ width for the table float[] width = new float[cols]; alIterator = alWidth.iterator(); int i = 0; while (alIterator.hasNext()) { float pixLength = ((Float) alIterator.next()).floatValue(); //alWidthRelativ.add( new Float( pixLength/totalWidth ) ); width[i] = pixLength / totalWidth; i++; } //needed for the shrink (enlarge?) float shrinkFactor; try { //1st: set correct outputstream PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream()); //1.5st: set content-stuff response.setContentType("application/pdf"); //2nd: set EventManager for PageEvents to Helper writer.setPageEvent( new CreateSubscriberListActionHelper(myForm.getHeaderLeft(), myForm.getHeaderRight())); //3rd: open document for editing the content document.open(); //4th: add content Phrase pInfoText = new Phrase(myForm.getInfoText(), new Font(bf, 12, Font.BOLD)); document.add(pInfoText); //PdfPTable( cols ) PdfPTable table = new PdfPTable(width); float documentWidth = document.right() - document.left(); if (documentWidth < totalWidth) { table.setTotalWidth(documentWidth); shrinkFactor = documentWidth / totalWidth; } else { table.setTotalWidth(totalWidth); shrinkFactor = 1; } table.setLockedWidth(true); Font headerFont = new Font(bf, TABLEHEADER_FONTSIZE * shrinkFactor, Font.BOLDITALIC); Font cellFont = new Font(bf, TABLECELL_FONTSIZE * shrinkFactor, Font.NORMAL); if (myForm.getShowNumber()) table.addCell(new Phrase(TABLEHEADER_NUMBER, headerFont)); if (myForm.getShowPersonalNumber()) table.addCell(new Phrase(TABLEHEADER_PERSONALNUMBER, headerFont)); if (myForm.getShowName()) table.addCell(new Phrase(TABLEHEADER_NAME, headerFont)); if (myForm.getShowEmail()) table.addCell(new Phrase(TABLEHEADER_EMAIL, headerFont)); if (myForm.getShowResult()) table.addCell(new Phrase(TABLEHEADER_RESULT, headerFont)); if (myForm.getAddInfoField()) table.addCell(new Phrase(TABLEHEADER_INFO, headerFont)); if (myForm.getAddSignField()) table.addCell(new Phrase(TABLEHEADER_SIGN, headerFont)); //fill table Iterator iSRL = cSubscriberResultList.iterator(); int counter = 1; while (iSRL.hasNext()) { UserResult curResult = (UserResult) iSRL.next(); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); if (myForm.getShowNumber()) table.addCell(new Phrase("" + counter++, cellFont)); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); if (myForm.getShowPersonalNumber()) table.addCell(new Phrase(curResult.getPersonalNumber(), cellFont)); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); if (myForm.getShowName()) table.addCell(new Phrase(curResult.getSurname() + ", " + curResult.getFirstName(), cellFont)); if (myForm.getShowEmail()) table.addCell(new Phrase(curResult.getEmailAddress(), cellFont)); if (myForm.getShowResult()) table.addCell(new Phrase(curResult.getResult(), cellFont)); if (myForm.getAddInfoField()) table.addCell(new Phrase("", cellFont)); if (myForm.getAddSignField()) table.addCell(new Phrase("", cellFont)); } //set how many rows are header... table.setHeaderRows(1); document.add(table); //5th: close document, write the output to the stream... document.close(); } catch (Exception de) { Log.addLog("CreateSubscriberListAction: Error creating PDF: " + de); } //we dont need to return a forward, because we write directly to the outputstream! return null; }
From source file:de.tr1.cooperator.manager.web.CreateSubscriberListActionHelper.java
License:Open Source License
/** * After the content of the page is written, we put page X / Y * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) *//*from w ww .jav a 2 s . c o m*/ public void onEndPage(PdfWriter writer, Document document) { int pageN = writer.getPageNumber(); String text = "" + pageN; String separator = " / "; float text_len = bf.getWidthPoint(text, 8); float sep_len = bf.getWidthPoint(separator, 8); cb.beginText(); cb.setFontAndSize(bf, 8); float absMiddle = (document.right() - document.left()) / 2 + document.left(); cb.setTextMatrix(absMiddle - text_len - (sep_len / 2), 30); cb.showText(text); cb.setTextMatrix(absMiddle - (sep_len / 2), 30); cb.showText(separator); cb.endText(); cb.addTemplate(template, absMiddle + (sep_len / 2), 30); // write the headertable tHeader.setTotalWidth(document.right() - document.left()); tHeader.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 20, cb); }
From source file:dinamica.PDFPageEvents.java
License:LGPL
/** * Create custom-made footer on every page, this * will place the empty template on every page */// w w w . ja va 2s . c o m public void onEndPage(PdfWriter writer, Document document) { // print (x of ...) on every page (bottom right) // append template at the end of the footer, like: // 1 of [template] - template is an empty box String footer = writer.getPageNumber() + this.pageXofY; float tWidth = bf.getWidthPoint(footer, 10); float extraSpace = bf.getWidthPoint("00", 10); float ty = document.bottom() - 12; //below bottom margin float tx = document.right() - tWidth - extraSpace; //x coordinate for the footer // print "X of " on right-side and footer + date on left side cb.beginText(); cb.setFontAndSize(bf, 10); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footer, tx, ty, 0); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footerLeft, document.left(), ty, 0); cb.endText(); // now append empty template after "X of " cb.addTemplate(tpl, document.right() - extraSpace, ty); }
From source file:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java
License:GNU General Public License
/** * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, * com.lowagie.text.Document)//from w ww. j a v a2 s . c o m */ public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // write the headertable table.setTotalWidth(document.right() - document.left()); table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 20, cb); // compose the footer String text = writer.getPageNumber() + " / "; float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 49; cb.beginText(); cb.setFontAndSize(helv, 12); float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust - 5, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); cb.saveState(); // draw a Rectangle around the page cb.setLineWidth(1); cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40); cb.stroke(); cb.restoreState(); }
From source file:gov.noaa.pfel.coastwatch.sgt.SgtUtil.java
License:Open Source License
/** * This closes the pdf file created by createPDF, after you have * written things to g2D.//from w w w.ja v a 2 s . co m * * @param oar the object[] returned from createPdf * @throwsException if trouble */ public static void closePdf(Object oar[]) throws Exception { Graphics2D g2D = (Graphics2D) oar[0]; Document document = (Document) oar[1]; PdfContentByte pdfContentByte = (PdfContentByte) oar[2]; PdfTemplate pdfTemplate = (PdfTemplate) oar[3]; g2D.dispose(); //center it if (verbose) String2.log("SgtUtil.closePdf" + " left=" + document.left() + " right=" + document.right() + " bottom=" + document.bottom() + " top=" + document.top() + " template.width=" + pdfTemplate.getWidth() + " template.height=" + pdfTemplate.getHeight()); //x device = ax user + by user + e //y device = cx user + dy user + f pdfContentByte.addTemplate(pdfTemplate, //a,b,c,d,e,f //x,y location in points 0.5f, 0, 0, 0.5f, document.left() + (document.right() - document.left() - pdfTemplate.getWidth() / 2) / 2, document.bottom() + (document.top() - document.bottom() - pdfTemplate.getHeight() / 2) / 2); /* //if boundingBox is small, center it //if boundingBox is large, shrink and center it //document.left/right/top/bottom include 1/2" margins float xScale = (document.right() - document.left()) / pdfTemplate.getWidth(); float yScale = (document.top() - document.bottom()) / pdfTemplate.getHeight(); float scale = Math.min(Math.min(xScale, yScale), 1); float xSize = pdfTemplate.getWidth() / scale; float ySize = pdfTemplate.getHeight() / scale; //x device = ax user + by user + e //y device = cx user + dy user + f pdfContentByte.addTemplate(pdfTemplate, //a,b,c,d,e,f scale, 0, 0, scale, document.left() + (document.right() - document.left() - xSize) / 2, document.bottom() + (document.top() - document.bottom() - ySize) / 2); */ document.close(); }
From source file:jm.web.Addons.java
License:GNU General Public License
public static void setEncabezado(PdfWriter writer, Document document, String texto) { if (writer.getPageNumber() > 1) { try {/*from ww w . j a va2 s . co m*/ PdfPTable encabezado = new PdfPTable(1); encabezado.setTotalWidth(document.right() - document.left() - 120); encabezado.addCell(Addons.setCeldaPDF(texto, Font.HELVETICA, 9, Font.BOLD, Element.ALIGN_LEFT, 0)); encabezado.writeSelectedRows(0, -1, 60, document.top() + 25, writer.getDirectContent()); PdfContentByte cb = writer.getDirectContent(); cb.setLineWidth(2); cb.moveTo(60, document.top() + 10); cb.lineTo(document.right() - document.left() - 58, document.top() + 10); } catch (Exception e) { throw new ExceptionConverter(e); } } }