List of usage examples for org.apache.pdfbox.pdmodel.font PDFont getStringWidth
public float getStringWidth(String text) throws IOException
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 . j a v a 2 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:Clavis.Windows.WShedule.java
/** * @see/*w w w. j a v a 2s . c om*/ * http://fahdshariff.blogspot.pt/2010/10/creating-tables-with-pdfbox.html */ private static void drawTable(PDDocument doc, String[][] content, String titulo, String subtitulo, String subsubtitulo) throws IOException { float y = 680f; float margin = 60f; final int rows = content.length; final int cols = content[0].length; final float rowHeight = 20f; int maximolinhas = (int) (y / (rowHeight + 2)); float tableHeight; int paginas = 0; int linhas; if (rows < maximolinhas) { linhas = rows; paginas++; } else { linhas = maximolinhas; int auxiliar = 0; while (auxiliar < rows) { paginas++; auxiliar += maximolinhas; } } final float cellMargin = 5f; float tamanhotexto = 12f; float tamanhotexto2 = 8f; float dimensao = 0f; float dimensao2 = 0f; PDFont font = PDType1Font.TIMES_ROMAN; for (String[] content1 : content) { dimensao = font.getStringWidth(content1[0]) / 1000 * tamanhotexto2; if (dimensao > 222.0f) { String nome = content1[0]; String[] texto = nome.split(" "); nome = nome.replace(texto[0], ""); nome = nome.replace(texto[texto.length - 1], ""); int i = 1; while (dimensao > 222.0f) { if (texto[i].length() > 2) { nome = nome.replace(texto[i], texto[i].charAt(0) + "."); } else { nome = nome.replace(texto[i], ""); } dimensao = font.getStringWidth(texto[0] + nome + texto[texto.length - 1]) / 1000 * tamanhotexto2; i++; } content1[0] = texto[0] + nome + texto[texto.length - 1]; } dimensao2 = font.getStringWidth(content1[3]) / 1000 * tamanhotexto2 + 10; if (dimensao2 > 180.0f) { String nome = content1[3]; String[] texto = nome.split(" "); nome = nome.replace(texto[0], ""); nome = nome.replace(texto[texto.length - 1], ""); int i = 1; while (dimensao2 > 180.0f) { if (texto[i].length() > 2) { nome = nome.replace(texto[i], texto[i].charAt(0) + "."); } else { nome = nome.replace(texto[i], ""); } dimensao2 = font.getStringWidth(texto[0] + nome + texto[texto.length - 1]) / 1000 * tamanhotexto2; i++; } content1[3] = texto[0] + nome + texto[texto.length - 1]; } } int passagem = 0; int passagemdepagina = 0; float tableWidth; if (dimensao < 100) { dimensao = 100; } if (dimensao2 < 100) { dimensao2 = 100; } float firstcolWidth = dimensao + 10 * cellMargin; float lastcolWidth = dimensao2 + 10 * cellMargin; float colWidth; PDPageContentStream contentStream = null; while (passagem < paginas) { PDPage page = new PDPage(); doc.addPage(page); try { contentStream = new PDPageContentStream(doc, page); } catch (IOException ex) { Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex); } if (contentStream != null) { contentStream.setFont(font, tamanhotexto); tableWidth = page.getMediaBox().getWidth() - (2 * margin); colWidth = (tableWidth - firstcolWidth - lastcolWidth) / (float) (cols - 2); if (passagem == 0) { contentStream.beginText(); float posicao = margin + (tableWidth / 2) - ((font.getStringWidth(titulo) / 1000 * tamanhotexto) / 2); contentStream.newLineAtOffset(posicao, page.getMediaBox().getHeight() - 40); contentStream.showText(titulo); contentStream.endText(); contentStream.beginText(); posicao = margin + (tableWidth / 2) - ((font.getStringWidth(subtitulo) / 1000 * tamanhotexto) / 2); contentStream.newLineAtOffset(posicao, page.getMediaBox().getHeight() - 60); contentStream.showText(subtitulo); contentStream.endText(); contentStream.beginText(); posicao = margin + (tableWidth / 2) - ((font.getStringWidth(subsubtitulo) / 1000 * tamanhotexto) / 2); contentStream.newLineAtOffset(posicao, page.getMediaBox().getHeight() - 80); contentStream.showText(subsubtitulo); contentStream.endText(); } if (passagem == 1) { y += 40; } float nexty = y; contentStream.setFont(font, tamanhotexto2); for (int i = 0; i <= linhas; i++) { if (i < linhas) { if ((i % 2) == 0) { contentStream.setNonStrokingColor(200, 200, 200); contentStream.addRect(margin, nexty - rowHeight, tableWidth, rowHeight); contentStream.fill(); } } contentStream.setNonStrokingColor(0, 0, 0); contentStream.moveTo(margin, nexty); contentStream.lineTo(margin + tableWidth, nexty); contentStream.stroke(); nexty -= rowHeight; } //draw the columns float nextx = margin; for (int i = 0; i <= cols; i++) { contentStream.moveTo(nextx, y); if (linhas < maximolinhas) { tableHeight = rowHeight * linhas; } else { tableHeight = rowHeight * maximolinhas; } contentStream.lineTo(nextx, y - tableHeight); contentStream.stroke(); switch (i) { case 0: nextx += firstcolWidth; break; case 3: nextx += lastcolWidth; break; default: nextx += colWidth; break; } } float textx = margin; float texty = y - 15; float ttexto; boolean primeira = true; for (int i = 0; i < linhas; i++) { for (int j = 0; j < content[i + passagemdepagina].length; j++) { String text = content[i + passagemdepagina][j]; ttexto = font.getStringWidth(text) / 1000 * tamanhotexto2; if (j == 0) { ttexto = textx + ((firstcolWidth / 2) - (ttexto / 2)); } else if (j < 3) { ttexto = textx + ((colWidth / 2) - (ttexto / 2)); } else { ttexto = textx + ((lastcolWidth / 2) - (ttexto / 2)); } contentStream.beginText(); contentStream.newLineAtOffset(ttexto, texty); contentStream.showText(text); contentStream.endText(); if (j == 0) { textx += firstcolWidth; } else { textx += colWidth; } } texty -= rowHeight; textx = margin; } contentStream.beginText(); contentStream.newLineAtOffset((tableWidth / 2) + margin, 40); contentStream.showText("" + (passagem + 1)); contentStream.endText(); contentStream.close(); } passagem++; maximolinhas = (int) (y / (rowHeight + 1)); linhas = rows - (maximolinhas * passagem); if (linhas > maximolinhas) { linhas = maximolinhas; } passagemdepagina = maximolinhas * passagem; } }
From source file:com.devnexus.ting.web.controller.PdfUtils.java
License:Apache License
float getStringWidth(String text, PDFont font, int fontSize) throws IOException { return font.getStringWidth(text) * fontSize / 1000F; }
From source file:com.fileOperations.StampPDF.java
/** * This stamps docketed files./*from w w w . j a v a 2 s . c om*/ * * @param file String (full file path) * @param docketTime Timestamp * @param dept */ public static void stampDocument(String file, Timestamp docketTime, String dept) { // the document PDDocument doc = null; try { PDFont stampFont = PDType1Font.TIMES_ROMAN; float stampFontSize = 14; String title = PDFBoxTools.HeaderTimeStamp(docketTime) + " " + dept; float titleWidth = stampFont.getStringWidth(title) / 1000 * stampFontSize; float titleHeight = stampFont.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * stampFontSize; int marginTop = 20; doc = PDDocument.load(new File(file)); if (!doc.isEncrypted()) { for (int i = 0; i < doc.getPages().getCount(); i++) { PDPageContentStream contentStream = null; PDPage page = (PDPage) doc.getPages().get(i); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true); page.getResources().getFontNames(); contentStream.beginText(); contentStream.setFont(stampFont, stampFontSize); contentStream.setNonStrokingColor(Color.RED); contentStream.newLineAtOffset((page.getMediaBox().getWidth() - titleWidth) / 2, page.getMediaBox().getHeight() - marginTop - titleHeight); contentStream.showText(title); contentStream.endText(); contentStream.close(); } doc.save(file); } } catch (IOException ex) { ExceptionHandler.Handle(ex); } finally { if (doc != null) { try { doc.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); } } } }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void title(String text, PDFont font, int fontSize, PDDocument doc) throws IOException { if (positionHeight + (page.getMediaBox().getHeight() / 6) >= page.getMediaBox().getHeight()) { if (positionTitleWidth > DEFAULT_TITLE_MARGIN_WIDTH) { initNewPage(doc);/*from w ww . j a va2s .c o m*/ } else { writeToRight(); } } // Jump line if not the first page title. if (positionHeight != DEFAULT_MARGIN_HEIGHT) { positionHeight += (titleSpaceHeight * 2); } PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText(); stream.setFont(font, fontSize); stream.newLineAtOffset(positionTitleWidth, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += titleSpaceHeight + textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static int centerText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText();// w w w . ja va2 s . co m stream.setFont(font, fontSize); stream.newLineAtOffset((page.getMediaBox().getWidth() - textWidth) / 2, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += textHeight; return (int) textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void rightText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText();//from w w w. j a v a 2 s .c o m stream.setFont(font, fontSize); stream.newLineAtOffset(page.getMediaBox().getWidth() - textWidth - DEFAULT_LOGO_RIGHT_MARGIN_WIDTH, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void leftText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText();/* w ww . j av a 2 s . com*/ stream.setFont(font, fontSize); stream.newLineAtOffset(DEFAULT_LOGO_LEFT_MARGIN_WIDTH, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static int maxSizeFont(String text, PDFont font, PDPage page) throws IOException { return (int) (page.getMediaBox().getWidth() / (font.getStringWidth(text) * 1.1 / 1000)); }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void attribute(PDImageXObject logo, int logoHeight, int logoWidth, String value, String label, PDFont font, int fontSize, PDDocument doc, String index, boolean isPercent, Color color) throws IOException { int dataFontSize = (int) (fontSize * 1.5f); int labelFontSize = fontSize; float logoYCoordinate = page.getMediaBox().getHeight() - positionHeight - logoHeight; float textWidth = (font.getStringWidth(value) / 1000 * dataFontSize) + (font.getStringWidth(label) / 1000 * labelFontSize); float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * dataFontSize; float textYCoordinate = page.getMediaBox().getHeight() - positionHeight - (logoHeight / 2) - (textHeight / 2);/*from www . ja v a2 s .c o m*/ float dataXCoordinate = positionLogoWidth + logoHeight + DEFAULT_SPACE_WIDTH; positionHeight += spaceHeight + ((logoHeight < textHeight) ? textHeight : logoHeight); if (positionHeight >= page.getMediaBox().getHeight()) { if (positionLogoWidth > DEFAULT_ICON_MARGIN_WIDTH) { initNewPage(doc); attribute(logo, logoHeight, logoWidth, value, label, font, labelFontSize, doc, index, isPercent, color); } else { writeToRight(); attribute(logo, logoHeight, logoWidth, value, label, font, labelFontSize, doc, index, isPercent, color); } } else { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); try { stream.drawImage(logo, positionLogoWidth, logoYCoordinate, logoWidth, logoHeight); stream.beginText(); stream.setFont(font, dataFontSize); stream.setNonStrokingColor(color); stream.newLineAtOffset(dataXCoordinate, textYCoordinate + 6); stream.showText(value); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); stream.setFont(font, labelFontSize); stream.showText(label); if (index != null) { stream.newLineAtOffset(textWidth, (int) (labelFontSize * 0.3)); stream.setFont(PDType1Font.COURIER_BOLD, (int) (labelFontSize * 0.8)); stream.showText("("); stream.setNonStrokingColor(color); stream.showText(index); if (isPercent) { stream.showText("%"); } stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); stream.showText(")"); } } finally { stream.endText(); stream.close(); } } }