List of usage examples for org.apache.pdfbox.pdmodel.font PDType1Font getStringWidth
public float getStringWidth(String text) throws IOException
From source file:com.foc.vaadin.gui.mswordGenerator.FocMSWordFontProperties.java
License:Apache License
public int getFontHeight(PDPageContentStream contentStream, PDType1Font font, int fontSize, String content) throws IOException { int start = 0; int end = 0;//from w w w . jav a2 s .c om int stringHeight = 10; if (content != null && !content.isEmpty()) { for (int i : possibleWrapPoints(content)) { float width = font.getStringWidth(content.substring(start, i)) / 1000 * fontSize; if (start < end/* && width > paragraphWidth*/) { contentStream.moveTextPositionByAmount(10, stringHeight); contentStream.drawString(toString().trim().substring(start, end)); stringHeight += font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; start = end; } end = i; } } return stringHeight; }
From source file:com.foc.vaadin.gui.mswordGenerator.FocMSWordFontProperties.java
License:Apache License
public int getFontWidth(PDPageContentStream contentStream, PDType1Font font, int fontSize, String content) throws IOException { int start = 0; int end = 0;/* w ww.j a v a 2 s . c o m*/ int stringWidth = 10; if (content != null && !content.isEmpty()) { for (int i : possibleWrapPoints(content)) { float width = font.getStringWidth(content.substring(start, i)) / 1000 * fontSize; if (start < end/* && width > paragraphWidth*/) { stringWidth += font.getStringWidth(content.substring(start, i)) / 1000 * fontSize; start = end; } end = i; } } return stringWidth; }
From source file:com.foc.vaadin.gui.mswordGenerator.MSWordUtil.java
License:Apache License
public static float getStringWidth(PDType1Font font, int fontSize, String content) { float stringWidth = 0; try {//from ww w.j a v a2 s . c o m stringWidth = font.getStringWidth(content) / 1000 * fontSize; } catch (IOException e) { e.printStackTrace(); } return stringWidth; }
From source file:name.marcelomorales.siqisiqi.pdfbox.CoordinatesGenerator.java
License:Apache License
public void generarPdf(OutputStream os, String template, Map<String, Object> m, String path, String coordenates, float fontSize, float ancho) throws IOException { long t = System.currentTimeMillis(); PDDocument doc = null;//from ww w . j a v a2 s.c om try { doc = PDDocument.load(new File(path)); List pages = doc.getDocumentCatalog().getAllPages(); PDPage sourcePage = (PDPage) pages.get(0); boolean append = sourcePage.getContents() != null; PDPageContentStream contentStream = new PDPageContentStream(doc, sourcePage, append, true); StringReader fileReader = null; try { fileReader = new StringReader(template); List<String> list = CharStreams.readLines(fileReader); boolean textHasBegun = false; float currentOffset = 0f; for (String line : list) { if (line == null) { continue; } if (line.startsWith("#")) { continue; } final Iterable<String> str = Splitter.on(',').omitEmptyStrings().trimResults().split(line); final String[] split = Iterables.toArray(str, String.class); if (split == null || split.length < 4) { continue; } if (Character.isDigit(split[0].charAt(0))) { if (textHasBegun) { contentStream.endText(); } contentStream.beginText(); textHasBegun = true; contentStream.moveTextPositionByAmount(parseFloat(split[0]), parseFloat(split[1])); } else { contentStream.moveTextPositionByAmount(currentOffset, 0); } if (!textHasBegun) { LOGGER.warn("Hay un posible mal uso de un .ree", new Throwable()); contentStream.beginText(); textHasBegun = true; } PDType1Font font; if ("b".equals(split[2])) { font = HELVETICA_BOLD; } else { font = HELVETICA; } contentStream.setFont(font, fontSize); Object text = null; if (split[3].startsWith("\"")) { // TODO: text = substring(split[3], 1, -1); } else { // TODO: text = new PropertyModel(m, split[3]).getObject(); } if (text == null) { LOGGER.warn("Propiedad {} no se encuentra", split[3]); //contentStream.drawString("ERROR: propiedad no encontrada"); contentStream.drawString(" "); } else { String string = text.toString(); currentOffset = font.getStringWidth(string) * ancho; contentStream.drawString(string); } } if (textHasBegun) { contentStream.endText(); } } finally { Closeables.closeQuietly(fileReader); } contentStream.close(); try { doc.save(os); } catch (COSVisitorException e) { throw new IOException("Ha ocurrido un error al escribir en el Os", e); } } finally { if (doc != null) { doc.close(); } LOGGER.info("Me ha tomado {} milisegundos hacer el pdf", System.currentTimeMillis() - t); } }