List of usage examples for org.apache.pdfbox.pdmodel PDPageContentStream setNonStrokingColor
@Deprecated public void setNonStrokingColor(float[] components) throws IOException
From source file:mail.java
private void bonos() throws SQLException { //**//w w w .j av a 2 s . c om double total = 0; ListaBono.clear(); if (idmatricula != "0") { Connection conn; try { conn = Conector.Connect(); System.out.println("Bonos"); PreparedStatement resultado; PreparedStatement bono; String sql1 = "select * from bonos b left join juzgados j on b.ID_JUZGADO=j.id_juzgado where ID_MATRICULA=? ORDER BY ANO "; bono = conn.prepareStatement(sql1); bono.setString(1, idmatricula); ResultSet ds = bono.executeQuery(); DecimalFormatSymbols simbolos = new DecimalFormatSymbols(); SimpleDateFormat formatofecha = new SimpleDateFormat("dd-MM-yyyy"); simbolos.setDecimalSeparator('.'); DecimalFormat decim = new DecimalFormat("0.00", simbolos); if (ds.first()) { ds.beforeFirst();//regresa el puntero al primer registro while (ds.next()) { int numero_expediente = ds.getInt("NUMERO_EXPTE"); String ano = ds.getString("ANO"); String caratula = ds.getString("cara"); String fecha_actuacion = ds.getString("FECHA_ACTUACION"); String juzgado = ds.getString("descripcion"); double importe = ds.getDouble("MONTOBONO"); Double monto_bono = Double.valueOf(decim.format(importe)); caratula = caratula.replaceAll("\r\n", " "); caratula = caratula.replaceAll("\n", " "); caratula = caratula.replaceAll("\\\\\\\\", ""); // Date per =formatofecha.parse(fecha_pa); // String fecha_pago = formatofecha.format(per); System.out.println(idmatricula); System.out.format("%s,%s,%s,%s,%s,%s\n", numero_expediente, ano, caratula.toLowerCase(), juzgado.toLowerCase(), fecha_actuacion, importe); ListaBono.add( new Bonos(numero_expediente, ano, caratula, juzgado, fecha_actuacion, monto_bono)); total = total + monto_bono; botones(true); } ds.close(); System.out.println(total); System.out.println(ListaBono.size()); } } catch (SQLException ex) { Logger.getLogger(mail.class.getName()).log(Level.SEVERE, null, ex); } } //** int alto = 595; int ancho = 842; PDDocument documento = new PDDocument(); PDPage paginablanco = new PDPage(new PDRectangle(ancho, alto)); documento.addPage(paginablanco); PDPageContentStream content; System.out.println(paginablanco.getMediaBox().getHeight() + "--" + paginablanco.getMediaBox().getWidth()); try { content = new PDPageContentStream(documento, paginablanco); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(50, alto - 20); content.showText("Consejo Profesional de Abogacia"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(450, alto - 20); Locale espanol = new Locale("es", "ES"); SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE MMMM d HH:mm:ss z yyyy", espanol); String fecha = dateFormat.format(new Date()); content.showText(fecha); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 6); content.newLineAtOffset(50, alto - 28); content.showText("Direccin: San Martin 457 - Formosa"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 10); content.newLineAtOffset(200, alto - 38); content.showText("BONOS DE ACCCIN LETRADA"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(200, alto - 45); content.showText("Estado de Gestin de bonos del Profesional"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 9); content.newLineAtOffset(50, alto - 60); // para que no tenga errores las tabla de mysql cotejamiento en utf8_bin al cargar en la PDF da error si esta con el tema de las content.showText("Matricula N: " + idmatricula + " Nombre: " + nombre + "," + apellido);// content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(50, alto - 75); content.showText("Item"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(70, alto - 75); content.showText("Expte"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(100, alto - 75); content.showText("Ao"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(130, alto - 75); content.showText("Caratula"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(500, alto - 75); content.showText("Juzgado"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(650, alto - 75); content.showText("Monto"); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(680, alto - 75); content.showText("Vencimiento"); content.endText(); content.setNonStrokingColor(Color.DARK_GRAY); content.addRect(25, 45, 700, 400); // content.fill(); int j = 0; int renglon = alto - 100; for (int i = 0; i < ListaBono.size(); i++) { content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); j++; content.newLineAtOffset(50, renglon); content.showText(String.valueOf(i + 1)); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(70, renglon); content.showText(String.valueOf(ListaBono.get(i).getNumero_expediente())); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(100, renglon); content.showText(ListaBono.get(i).getAno()); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(130, renglon); content.showText(String.valueOf(ListaBono.get(i).getCaratula())); content.endText(); content.setFont(PDType1Font.COURIER, 8); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(500, renglon); content.showText(ListaBono.get(i).getJuzgado()); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(650, renglon); content.showText(String.valueOf(ListaBono.get(i).getMonto_bono())); content.endText(); content.beginText(); content.setFont(PDType1Font.HELVETICA, 8); content.newLineAtOffset(680, renglon); content.showText(ListaBono.get(i).getFecha_pago()); content.endText(); renglon = renglon - 13; } content.beginText(); content.newLineAtOffset(100, renglon - 20); DecimalFormatSymbols simbolos = new DecimalFormatSymbols(); simbolos.setDecimalSeparator('.'); DecimalFormat decim = new DecimalFormat("0.00", simbolos); content.showText("Total Bonos Adeudados :$ " + String.valueOf(decim.format(total))); content.endText(); content.close(); documento.save("matricula_" + idmatricula + "_bonos.pdf"); documento.close(); System.out.println("guardo archivo matricula_" + idmatricula + "_bonos.pdf"); // System.out.println(String.valueOf(decim.format(total))); EnviarMail.setEnabled(true); } catch (IOException ex) { EnviarMail.setEnabled(false); Logger.getLogger(mail.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.TableDrawUtils.java
License:EUPL
public static void drawTableBackground(PDPage page, PDPageContentStream contentStream, float x, float y, float width, float height, PDFBoxTable abstractTable, ISettings settings) throws PdfAsException { try {//from w w w. j ava2s . co m if (abstractTable.getBGColor() != null) { contentStream.setNonStrokingColor(abstractTable.getBGColor()); contentStream.fillRect(x, y, abstractTable.getWidth(), abstractTable.getHeight()); contentStream.setNonStrokingColor(Color.BLACK); } } catch (Throwable e) { logger.warn("drawing table borders", e); throw new PdfAsException("drawing table borders", e); } }
From source file:boxtable.cell.Cell.java
License:Apache License
/** * Renders this cell at the specified position * /*from w w w .ja v a 2 s. co m*/ * @param document * The document this cell is rendered to * @param stream * The PDPageContentStream used to render this cell * @param left * The coordinate of the left edge of the cell * @param top * The coordinate of the top edge of the cell * @param width * The width the cell will be rendered * @param height * The height the cell will be rendered * @throws IOException * If writing to the stream fails */ public void render(final PDDocument document, final PDPageContentStream stream, final float left, final float top, final float width, final float height) throws IOException { if (background != null) { stream.setNonStrokingColor(background); stream.addRect(left, top - height, width, height); stream.fill(); } drawBorder(stream, left, top, width, height); }
From source file:boxtable.cell.TextCell.java
License:Apache License
@Override public void render(final PDDocument document, final PDPageContentStream stream, final float left, final float top, final float width, final float height) throws IOException { final float fontSize = getFontSize(width, height); final float textHeight = font.getFontDescriptor().getAscent() / 1000 * fontSize; super.render(document, stream, left, top, width, height); stream.setNonStrokingColor(Color.BLACK); if (equallySpaced) { stream.setFont(font, fontSize);//from www . j av a 2 s .c om final float xStart = left + leftBorder / 2 + leftPadding; final float yPos = top - textHeight - font.getFontDescriptor().getDescent() / 1000 * fontSize - (height - textHeight) * vAlign - topBorder - topPadding - fontSize / 15; final float partialWidth = (width - leftPadding - rightPadding - (leftBorder + rightBorder) / 2) / (text.size() + 1); for (int i = 0; i < text.size(); ++i) { float xPos = xStart + (i + 1) * partialWidth - text.get(i).getWidth(font, fontSize) / 2; stream.beginText(); stream.newLineAtOffset(xPos, yPos); stream.showText(text.get(i).getText()); stream.endText(); } } else { final List<List<Text>> lines = breakText(width, fontSize); if (drawRows) { stream.setLineWidth(bottomBorder); for (int i = 0; i < lines.size(); ++i) { stream.moveTo(left + leftBorder / 2, top - height - bottomBorder / 2 + i * height / lines.size()); stream.lineTo(left + width - rightBorder / 2, top - height - bottomBorder / 2 + i * height / lines.size()); } stream.stroke(); } final float xStart = left + leftBorder + leftPadding; final float yStart = top - font.getFontDescriptor().getDescent() / 1000 * fontSize - topBorder / 2 - topPadding - fontSize / 15; for (int i = 0; i < lines.size(); ++i) { final List<Text> currentLine = lines.get(i); final float lineWidth = getLineWidth(currentLine, fontSize); float xPos = xStart + (width - lineWidth) * hAlign; final float yPos = yStart - textHeight * (i + 1) - i * fontSize / 10 - (height - textHeight * lines.size() - (lines.size() - 1) * fontSize / 10) * vAlign; for (int j = 0; j < currentLine.size(); ++j) { final Text current = currentLine.get(j); PDFont textFont = current.getFont() != null ? current.getFont() : font; float textSize = current.getFontSize() > 0 ? current.getFontSize() : fontSize; stream.beginText(); stream.newLineAtOffset(xPos, yPos + current.getVerticalOffset()); stream.setFont(textFont, textSize); if (j != 0) { stream.showText(" "); xPos += getTextWidth(" ", textFont, textSize); } float textWidth = getTextWidth(current.getText(), textFont, textSize); stream.showText(current.getText()); stream.endText(); if (current.isStriked()) { drawLine(stream, xPos, yPos + current.getVerticalOffset() + font.getFontDescriptor().getXHeight() / 1000 * fontSize / 2, textWidth); } if (current.isUnderlined()) { drawLine(stream, xPos, yPos + current.getVerticalOffset() - fontSize / 10, textWidth); } xPos += textWidth; } } } }
From source file:Bulletin.Bulletin2.java
private float BoxCenteredFilled(PDPageContentStream cos, String text, PDFont font, int size, float x, float y, float w, float h, Color col) { try {//w w w . j av a 2 s . co m cos.setNonStrokingColor(col); cos.fillRect(x, y - h, w, h); cos.stroke(); cos.setNonStrokingColor(Color.BLACK); this.BoxCentered(cos, text, font, size, x, y, w, h); } catch (IOException ex) { Logger.getLogger(Bulletin2.class.getName()).log(Level.SEVERE, null, ex); } return h; }
From source file:cdiscisa.StreamUtil.java
private static void imprimirDC3_individual(Participante p, Curso c, String chkDC3Firmaa, String chkDC3Logoa, String savePath, Map<String, String> dosc, Map<String, String> abreviaturas) throws IOException { //JOptionPane.showMessageDialog(null, "entrando a imrpimir individual"); PDDocument document;/*from www . j a v a 2 s .com*/ //BufferedInputStream file; InputStream file = null; try { file = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/DC3_blank.pdf"); //file = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/DC3_base_firma.pdf"); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error al cargar el una forma DC3 \nfile: " + String.valueOf(file) + "\n" + ex.toString()); } document = PDDocument.load(file); BufferedImage logo = null; BufferedImage firma = null; try { //logo = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/logo.png").getFile()); logo = ImageIO.read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/logo.png")); //logo = StreamUtil.stream2file(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/logo.png")); if (c.capacitador.equalsIgnoreCase("Ing. Jorge Antonio Razn Gutierrez")) { //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaCoco.png").getFile()); firma = ImageIO .read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaCoco.png")); } else if (c.capacitador.equalsIgnoreCase("Manuel Anguiano Razn")) { firma = ImageIO.read( cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaManuel.png")); //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaManuel.png").getFile()); } else { firma = ImageIO .read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaJorge.png")); //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaJorge.png").getFile()); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error al cargar la imagen del logo o la firma \nfile: " + String.valueOf(logo) + "\n" + String.valueOf(firma) + "\n" + ex.toString()); } PDImageXObject firmaObject = null; PDImageXObject logoObject = null; try { if (chkDC3Firmaa.equalsIgnoreCase("true")) { firmaObject = LosslessFactory.createFromImage(document, firma); //firmaObject = PDImageXObject.createFromFile(firma, document); } if (chkDC3Logoa.equalsIgnoreCase("true")) { logoObject = LosslessFactory.createFromImage(document, logo); //logoObject = PDImageXObject.createFromFile(logo, document); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error al crear objetos de logo o firma \nfile: " + String.valueOf(logoObject) + "\n" + String.valueOf(firmaObject) + "\n" + ex.toString()); } PDPage page = (PDPage) document.getDocumentCatalog().getPages().get(0); PDFont helvetica = PDType1Font.HELVETICA_BOLD; PDFont helvetica_normal = PDType1Font.HELVETICA; PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true); contentStream.beginText(); contentStream.setFont(helvetica_normal, 13); contentStream.setNonStrokingColor(Color.BLACK); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 30, 602)); contentStream.showText(p.apellidos + " " + p.nombre); String temp; if (p.curp.length() > 18) { temp = p.curp.substring(0, 18); } else { temp = p.curp; } char[] curpArray = temp.toCharArray(); float[] xPosition = { 32, 49, 63, 77, 92, (float) 106.5, 120, (float) 134.5, 149, 163, 177, (float) 191.5, 205, 223, 241, 255, 269, 286 }; float charWidth; for (int i = 0; i <= curpArray.length - 1; i++) { charWidth = helvetica_normal.getStringWidth(String.valueOf(curpArray[i])) / 1000 * 13; //System.out.println("Char " + i + " width: " + charWidth); float x = xPosition[i] - (charWidth / 2); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, x, 572)); contentStream.showText(String.valueOf(curpArray[i])); } contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 30, 548)); contentStream.showText(p.area_puesto); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 30, 486)); if (c.walmart) { contentStream.showText("OPERADORA WALMART S DE RL DE CV"); } else { contentStream.showText(c.nombre_empresa); } char[] rfc_emprea_array; if (c.walmart) { rfc_emprea_array = "OWM011023AWA".toCharArray(); } else { rfc_emprea_array = c.rfc_empresa.toCharArray(); } try { int j = 0; for (int i = 0; i <= rfc_emprea_array.length - 1; i++) { charWidth = helvetica_normal .getStringWidth(String.valueOf(rfc_emprea_array[rfc_emprea_array.length - i - 1])) / 1000 * 13; //System.out.println("Char " + i + " width: " + charWidth); if (j == 3 || i == 9) { j = j + 1; } float x = xPosition[xPosition.length - j - 4] - (charWidth / 2); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, x, 458)); contentStream.showText(String.valueOf(rfc_emprea_array[rfc_emprea_array.length - i - 1])); j = j + 1; } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "El RFC de la empresa esta mal formado. \n\nex.toString : " + ex.toString()); return; } contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 30, 405)); contentStream.showText(c.nombre_curso); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 30, 381)); contentStream.showText(c.horas_texto); //if (c.walmart){ contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 30, 356)); contentStream.showText("6000 SEGURIDAD"); //} else{ // contentStream.setTextMatrix(new Matrix(1,0,0,1,30,356 )); // contentStream.showText(p.area_tematica); //} contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 30, 332)); contentStream.showText(c.uCapacitadora); Calendar cal = Calendar.getInstance(); cal.setTime(c.fecha_inicio); String month = String.format("%02d", cal.get(Calendar.MONTH) + 1); String year = String.format("%04d", cal.get(Calendar.YEAR)); String day = String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)); String date = year + month + day; cal.setTime(c.fecha_termino); month = String.format("%02d", cal.get(Calendar.MONTH) + 1); year = String.format("%04d", cal.get(Calendar.YEAR)); day = String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)); date = date + year + month + day; char[] date_array = date.toCharArray(); float[] xPos = { 256, 272, 288, 304, 322, 343, 365, 386, 428, 447, 467, 486, 507, 528, 549, 570 }; for (int i = 0; i <= date_array.length - 1; i++) { charWidth = helvetica_normal.getStringWidth(String.valueOf(date_array[i])) / 1000 * 13; //System.out.println("Char " + i + " width: " + charWidth); float x = xPos[i] - (charWidth / 2); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, x, 381)); contentStream.showText(String.valueOf(date_array[i])); } charWidth = helvetica_normal.getStringWidth(p.area_tematica) / 1000 * 13; //System.out.println(p.area_tematica + " bold : " + charWidth); if (charWidth >= 255) { contentStream.setFont(helvetica_normal, 10); charWidth = helvetica_normal.getStringWidth(p.area_tematica) / 1000 * 10; //System.out.println(p.area_tematica + " : " + charWidth); if (charWidth >= 260) { contentStream.setFont(helvetica_normal, 8); //charWidth = helvetica_normal.getStringWidth(p.area_tematica) /1000 * 8; //System.out.println(p.area_tematica + " : " + charWidth); } } contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 310, 572)); contentStream.showText(p.area_tematica); contentStream.endText(); //xPosition = 256, 272, 288, 304, 322, 343, 365, 386, 428, 447, 467, 486, 507, 528, 549, 570 //year: 256, 272, 288, 304 //month: 322, 343 //day: 365, 386 //year: 428, 447, 467, 486 //month: 507, 528 //day: 549, 570 // 32, 49, 63, 77, 92, 106.5, 120, 134.5, 149, 163, 177, 191.5, 205, 223, 241, 255, 269, 286 /*Esto es para medir las casillas del curp contentStream.setStrokingColor(Color.BLACK); contentStream.moveTo(286,569); contentStream.lineTo(286,580); contentStream.stroke(); */ /* PDImageXObject logo = PDImageXObject.createFromFile("src/files/logo.png", document); PDImageXObject firma = PDImageXObject.createFromFile("src/files/firmasola.png", document); System.out.println("logoWidth: " + logo.getWidth()); System.out.println("logoHeight: " + logo.getHeight()); System.out.println("firmaWidth: " + firma.getWidth()); System.out.println("firmaHeight: " + firma.getHeight()); */ contentStream.addRect(50, 750, 500, 100); contentStream.setNonStrokingColor(Color.WHITE); contentStream.fill(); contentStream.addRect(50, 224, 150, 10); contentStream.setNonStrokingColor(Color.WHITE); contentStream.fill(); if (logoObject != null && !logoObject.isEmpty()) { contentStream.drawImage(logoObject, 430, 700, 150, 75); } if (firmaObject != null && !firmaObject.isEmpty()) { contentStream.drawImage(firmaObject, 80, 223, 110, 42); contentStream.setStrokingColor(Color.BLACK); contentStream.setLineWidth((float) .8); contentStream.moveTo(50, (float) 235.6); contentStream.lineTo(200, (float) 235.6); contentStream.stroke(); } contentStream.beginText(); contentStream.setFont(helvetica_normal, 8); contentStream.setNonStrokingColor(Color.BLACK); charWidth = helvetica_normal.getStringWidth(c.capacitador) / 1000 * 8; float x = 126 - charWidth / 2; contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, x, 228)); contentStream.showText(c.capacitador); //GIS100219KK8003 String regUnidad; if (c.uCapacitadora.equalsIgnoreCase("TSI. Jorge Antonio Razn Gil")) { regUnidad = "RAGJ610813BIA005"; } else { regUnidad = "GIS100219KK8003"; } charWidth = helvetica_normal.getStringWidth(regUnidad) / 1000 * 8; x = 126 - charWidth / 2; contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, x, 219)); contentStream.showText(regUnidad); contentStream.endText(); // Make sure that the content stream is closed: contentStream.close(); Format formatter = new SimpleDateFormat("ddMMMYYYY", new Locale("es", "MX")); String formatedDate = formatter.format(c.fecha_inicio); String abrev = abreviaturas.get(c.nombre_curso); // Save the results and ensure that the document is properly closed: document.save(savePath + File.separator + "p_DC3_" + p.curp + "_" + p.determinante + "_" + p.nombre.replaceAll(" ", "_") + "_" + p.apellidos.replaceAll(" ", "_") + "_" + abrev + "_" + formatedDate + ".pdf"); document.close(); dosc.put(savePath + File.separator + "p_DC3_" + p.curp + "_" + p.determinante + "_" + p.nombre.replaceAll(" ", "_") + "_" + p.apellidos.replaceAll(" ", "_") + "_" + abrev + "_" + formatedDate + ".pdf", p.determinante); }
From source file:cdiscisa.StreamUtil.java
private static void imprimirUnaConstancia(ArrayList<Participante> listaParticipantes, Curso c, Directorio d, String chkConstFirma, String chkConstLogo, String savePath, Map<String, String> dosc, String instructor, Map<String, String> abreviaturas) throws IOException { String contanciaTemplate = ""; switch (c.nombre_curso) { case "PREVENCIN Y COMBATE DE INCENDIOS I": contanciaTemplate = "files/certificado_vacio_incendio_basico_nf_nl.pdf"; break; case "BUSQUEDA Y RESCATE": contanciaTemplate = "files/certificado_vacio_busq_rescate_nf_nl.pdf"; break; case "EVACUACIN, BUSQUEDA Y RESCATE": contanciaTemplate = "files/certificado_vacio_evac_busq_resc_nf_nl.pdf"; break; case "EVACUACIN": contanciaTemplate = "files/certificado_vacio_evacuacion_nf_nl.pdf"; break; case "PREVENCIN Y COMBATE DE INCENDIOS II": contanciaTemplate = "files/certificado_vacio_incendio_intermedio_nf_nl.pdf"; break; case "PREVENCIN Y COMBATE DE INCENDIOS III": contanciaTemplate = "files/certificado_vacio_incendio_avanzado_nf_nl.pdf"; break; case "FORMACION DE BRIGADAS MULTIFUNCIONALES DE EMERGENCIA": contanciaTemplate = "files/certificado_vacio_multi_nf_nl.pdf"; break; case "FORMACIN DE BRIGADA MULTIFUNCIONAL DE EMERGENCIA": contanciaTemplate = "files/certificado_vacio_multi_nf_nl.pdf"; break; case "FORMACION DE BRIGADA MULTIFUNCIONAL DE EMERGENCIAS": contanciaTemplate = "files/certificado_vacio_multi_nf_nl.pdf"; break; case "PRIMEROS AUXILIOS": contanciaTemplate = "files/certificado_vacio_primeros_auxilios_nf_nl.pdf"; break; default://from w w w . ja va2 s. c o m contanciaTemplate = "files/cerificado_vacio.pdf"; break; } InputStream file = null; try { file = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream(contanciaTemplate); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error al cargar el certificado base. \nconstancia: " + contanciaTemplate + "\nfile: " + String.valueOf(file) + "\n" + ex.toString()); } PDDocument document = PDDocument.load(file); PDPage page1 = (PDPage) document.getDocumentCatalog().getPages().get(0); PDPage page2 = (PDPage) document.getDocumentCatalog().getPages().get(1); PDPageContentStream contentStream = new PDPageContentStream(document, page1, true, true); PDPageContentStream contentStream2 = new PDPageContentStream(document, page2, true, true); float pageWidth = page1.getMediaBox().getWidth(); BufferedImage logo = null; BufferedImage firma = null; try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); //logo = new File(classLoader.getResource("files/logo.png").getFile()); //logo = StreamUtil.stream2file(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/logo.png")); logo = ImageIO.read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/logo.png")); if (instructor.equalsIgnoreCase("Ing. Jorge Antonio Razn Gutierrez")) { firma = ImageIO .read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaCoco.png")); //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaCoco.png").getFile()); } else if (instructor.equalsIgnoreCase("Manuel Anguiano Razn")) { firma = ImageIO.read( cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaManuel.png")); //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaManuel.png").getFile()); } else { firma = ImageIO .read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaJorge.png")); //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaJorge.png").getFile()); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error al cargar la imagen del logo o la firma \nfile: " + String.valueOf(logo) + "\n" + String.valueOf(firma) + "\n" + ex.toString()); } PDImageXObject firmaObject = null; PDImageXObject logoObject = null; try { if (chkConstFirma.equalsIgnoreCase("true")) { firmaObject = LosslessFactory.createFromImage(document, firma); //firmaObject = PDImageXObject.createFromFile(firma, document); } if (chkConstLogo.equalsIgnoreCase("true")) { logoObject = LosslessFactory.createFromImage(document, logo); //logoObject = PDImageXObject.createFromFile(logo, document); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error al crear objetos de logo o firma \nfile: " + String.valueOf(logoObject) + "\n" + String.valueOf(firmaObject) + "\n" + ex.toString()); } InputStream isFont1 = null, isFont2 = null; try { isFont1 = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/Calibri.ttf"); isFont2 = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/CalibriBold.ttf"); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Error al cargar el una fuente \nisFont1: " + String.valueOf(isFont1) + "\nisFont2: " + String.valueOf(isFont2) + "\n" + ex.toString()); } PDFont calibri = PDType0Font.load(document, isFont1); PDFont calibriBold = PDType0Font.load(document, isFont2); contentStream.beginText(); DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, new Locale("es", "MX")); contentStream.setFont(calibri, 9); contentStream.setNonStrokingColor(Color.BLACK); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 465, 656)); contentStream.showText(df.format(new Date())); contentStream.setFont(calibriBold, 11); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 135, 585)); contentStream.showText(c.razon_social); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 120, (float) 572.5)); if (c.walmart) { contentStream.showText(d.determinante + " " + d.unidad); } else if (d.sucursal.isEmpty() || d.sucursal.equalsIgnoreCase("")) { contentStream.endText(); contentStream.addRect(30, 572, 150, 10); contentStream.setNonStrokingColor(Color.WHITE); contentStream.fill(); contentStream.beginText(); contentStream.setNonStrokingColor(Color.BLACK); } else { contentStream.showText(d.sucursal); } if (!c.walmart) { contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 527)); contentStream.showText(d.RFC); contentStream.setFont(calibri, 11); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 72, 527)); contentStream.showText("RFC: "); contentStream.setFont(calibriBold, 11); } /* if (c.walmart){ contentStream.showText(p1.determinante + " " + d.unidad); } else if (d.sucursal.isEmpty() || d.sucursal.equalsIgnoreCase("")){ contentStream.endText(); contentStream.addRect(100, 590, 400, 20); contentStream.setNonStrokingColor(Color.WHITE); contentStream.fill(); contentStream.beginText(); contentStream.setNonStrokingColor(Color.BLACK); } else{ contentStream.showText(d.sucursal); } */ float charWidth = calibriBold.getStringWidth(d.direccion) / 1000 * 11; //System.out.println(charWidth + " " + d.direccion.length() + " " + d.direccion); if (charWidth <= 400) { contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 549)); contentStream.showText(d.direccion); } else { contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 552)); contentStream.showText(d.direccion.substring(0, d.direccion.indexOf(" ", 82))); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 541)); contentStream.showText(d.direccion.substring(d.direccion.indexOf(" ", 82) + 1, d.direccion.length())); } charWidth = calibriBold.getStringWidth(c.nombre_curso) / 1000 * 11; float xPosition = (pageWidth - charWidth) / 2; contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 490)); contentStream.showText(c.nombre_curso); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 160, 465)); contentStream.showText(c.fecha_certificado); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 160, (float) 450.5)); contentStream.showText(c.horas_texto); ListIterator<Participante> it = listaParticipantes.listIterator(); float y = 0; while (it.hasNext()) { Participante p = it.next(); if (p.determinante.equalsIgnoreCase(d.determinante)) { contentStream.setFont(calibri, 11); charWidth = calibri.getStringWidth(p.nombre + " " + p.apellidos) / 1000 * 11; //System.out.println(charWidth + " " + p.nombre + " " + p.apellidos); if (charWidth > 165) { contentStream.setFont(calibri, 9); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 135, (float) 376.5 - y)); contentStream.showText(p.nombre + " " + p.apellidos); contentStream.setFont(calibri, 11); } else { contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 135, (float) 376.5 - y)); contentStream.showText(p.nombre + " " + p.apellidos); } charWidth = calibri.getStringWidth(p.area_puesto) / 1000 * 11; //System.out.println(charWidth + " " + p.area_puesto); if (charWidth > 112) { contentStream.setFont(calibri, 9); contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 360, (float) 376.5 - y)); contentStream.showText(p.area_puesto); } else { contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 360, (float) 376.5 - y)); contentStream.showText(p.area_puesto); } y = y + (float) 12.7; } } contentStream.endText(); float nameWidth; if (logoObject != null && !logoObject.isEmpty()) { contentStream.drawImage(logoObject, 30, 700, 156, 78); } if (firmaObject != null && !firmaObject.isEmpty()) { xPosition = (pageWidth - 100) / 2; contentStream.drawImage(firmaObject, xPosition, 55, 110, 42); contentStream.beginText(); contentStream.setFont(calibriBold, 10); contentStream.setNonStrokingColor(Color.BLACK); nameWidth = calibriBold.getStringWidth(instructor) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 55)); contentStream.showText(instructor); if (instructor.equalsIgnoreCase("Manuel Anguiano Razn")) { nameWidth = calibriBold.getStringWidth(c.registro_manuel) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 40)); contentStream.showText(c.registro_manuel); } else if (instructor.equalsIgnoreCase("Ing. Jorge Antonio Razn Gutierrez")) { nameWidth = calibriBold.getStringWidth(c.registro_coco) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 40)); contentStream.showText(c.registro_coco); } else { nameWidth = calibriBold.getStringWidth(c.registro_jorge) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 40)); contentStream.showText(c.registro_jorge); } contentStream.endText(); } // Make sure that the content stream is closed: contentStream.close(); contentStream2.beginText(); contentStream2.setFont(calibri, 11); contentStream2.setNonStrokingColor(Color.BLACK); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 465, 656)); contentStream2.showText(df.format(new Date())); contentStream2.setFont(calibriBold, 11); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 135, 585)); contentStream2.showText(c.razon_social); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 120, (float) 572.5)); if (c.walmart) { contentStream2.showText(d.determinante + " " + d.unidad); } else { contentStream2.showText(d.sucursal); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 527)); contentStream2.showText(d.RFC); contentStream2.setFont(calibri, 11); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 73, 527)); contentStream2.showText("RFC: "); contentStream2.setFont(calibriBold, 11); } charWidth = calibriBold.getStringWidth(d.direccion) / 1000 * 11; //System.out.println(charWidth + " " + d.direccion.length() + " " + d.direccion); if (charWidth <= 400) { contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 549)); contentStream2.showText(d.direccion); } else { contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 555)); contentStream2.showText(d.direccion.substring(0, d.direccion.indexOf(" ", 82))); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 120, 540)); contentStream2.showText(d.direccion.substring(d.direccion.indexOf(" ", 82) + 1, d.direccion.length())); } charWidth = calibriBold.getStringWidth(c.nombre_curso) / 1000 * 11; pageWidth = page2.getMediaBox().getWidth(); xPosition = (pageWidth - charWidth) / 2; contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 490)); contentStream2.showText(c.nombre_curso); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 160, 465)); contentStream2.showText(c.fecha_certificado); contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, 160, (float) 450.5)); contentStream2.showText(c.horas_texto); contentStream2.endText(); if (logoObject != null && !logoObject.isEmpty()) { contentStream2.drawImage(logoObject, 30, 700, 156, 78); } if (firmaObject != null && !firmaObject.isEmpty()) { xPosition = (pageWidth - 100) / 2; contentStream2.drawImage(firmaObject, xPosition, 55, 110, 42); contentStream2.beginText(); contentStream2.setFont(calibriBold, 10); contentStream2.setNonStrokingColor(Color.BLACK); nameWidth = calibriBold.getStringWidth(instructor) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 55)); contentStream2.showText(instructor); if (instructor.equalsIgnoreCase("Manuel Anguiano Razn")) { nameWidth = calibriBold.getStringWidth(c.registro_manuel) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 40)); contentStream2.showText(c.registro_manuel); } else if (instructor.equalsIgnoreCase("Ing. Jorge Antonio Razn Gutierrez")) { nameWidth = calibriBold.getStringWidth(c.registro_coco) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 40)); contentStream2.showText(c.registro_coco); } else { nameWidth = calibriBold.getStringWidth(c.registro_jorge) / 1000 * 10; xPosition = (pageWidth - nameWidth) / 2 + 9; contentStream2.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 40)); contentStream2.showText(c.registro_jorge); } contentStream2.endText(); } contentStream2.close(); //"Capacitacion_BAE_Centro de Huinala_2631_MULTI_19ago2015" //Capacitacin + formato tienda + nombre sucursal + numero sucursal + nombre curso + ddmmaaaa Format formatter = new SimpleDateFormat("ddMMMYYYY", new Locale("es", "MX")); String formatedDate = formatter.format(c.fecha_inicio); String abrev = abreviaturas.get(c.nombre_curso); // Save the results and ensure that the document is properly closed: if (c.walmart) { document.save(savePath + File.separator + "Certificado_" + d.formato + "_" + d.unidad + "_" + d.determinante + "_" + abrev + "_" + formatedDate + ".pdf"); document.close(); dosc.put(savePath + File.separator + "Certificado_" + d.formato + "_" + d.unidad + "_" + d.determinante + "_" + abrev + "_" + formatedDate + ".pdf", d.determinante); } else { document.save(savePath + File.separator + "Certificado_" + d.determinante + "_" + abrev + "_" + formatedDate + ".pdf"); document.close(); dosc.put(savePath + File.separator + "Certificado_" + d.determinante + "_" + abrev + "_" + formatedDate + ".pdf", d.determinante); } }
From source file:com.baseprogramming.pdwriter.HtmlPdWriter.java
License:Apache License
protected void writeText(PdParagraph style, String content) throws IOException { if (writer.isAtEndOfPage()) { writer.createNewPage();/*from w ww .j a va2 s .c om*/ xPosition = style.getLeftX(); } PDPageContentStream stream = writer.createStream(); try { stream.setFont(style.getFont(), style.getFontSize()); stream.setNonStrokingColor(style.getFontColor()); float yPosition = writer.getLastYPosition(); int lastPos = 0; boolean firstLine = true; while (true) { int start = lastPos; int end = content.indexOf("\n", start); if (end < 0) { end = style.getWrapPositionFromOffset(start, xPosition, content); } String string = content.substring(start, end); writer.writeText(stream, xPosition, yPosition, string); firstLine = false; lastPos = end; updateXPosition(style, string); if (end >= content.length()) { break; } yPosition = style.getNextY(yPosition); xPosition = style.getLeftX(); writer.setLastYPosition(yPosition); if (writer.isAtEndOfPage()) { stream = writer.createNewPageAndContentStream(stream, style); yPosition = writer.getLastYPosition(); xPosition = style.getLeftX(); } } writer.setLastYPosition(yPosition); } finally { if (stream != null) { stream.close(); } } }
From source file:com.baseprogramming.pdwriter.PdWriter.java
License:Apache License
/** * Write the content/* w ww . j a va 2s .com*/ * @param paragraph * @param content content to write * @throws java.io.IOException */ public void write(PdParagraph paragraph, String content) throws IOException { if (isAtEndOfPage()) { createNewPage(); } PDPageContentStream stream = createStream(); try { stream.setFont(paragraph.getFont(), paragraph.getFontSize()); stream.setNonStrokingColor(paragraph.getFontColor()); yPosition = paragraph.getUpperY(yPosition); int lastPos = 0; boolean firstLine = true; while (true) { int start = lastPos; boolean foundLineFeed = true; int end = content.indexOf("\n", start); if (end < 0) { end = paragraph.getWrapPosition(content, start, firstLine); foundLineFeed = false; } if (end > content.length()) { end = content.length(); } String string = content.substring(start, end); float xPosition = paragraph.getLeftX(firstLine); writeText(stream, xPosition, yPosition, string); firstLine = false; lastPos = end; if (foundLineFeed) { lastPos++; } yPosition = paragraph.getNextY(yPosition); if (end >= content.length()) { break; } if (isAtEndOfPage()) { stream = createNewPageAndContentStream(stream, paragraph); } } } finally { yPosition -= (paragraph.getLineSpacing() + paragraph.getBelowSpacing().getPoints()); if (stream != null) { stream.close(); } } }
From source file:com.baseprogramming.pdwriter.PdWriter.java
License:Apache License
public PDPageContentStream createStream(PdParagraph style) throws IOException { PDPageContentStream stream = createStream(); stream.setFont(style.getFont(), style.getFontSize()); stream.setNonStrokingColor(style.getFontColor()); return stream; }