List of usage examples for org.apache.pdfbox.pdmodel.font PDFont getStringWidth
public float getStringWidth(String text) throws IOException
From source file:com.przemo.pdfmanipulate.PDFBuilder.java
private static void renderForm(Formularz form, PDDocument doc) throws IOException { int i = 0;/*from www .j av a 2 s . com*/ PDFont f = PDType1Font.HELVETICA; float fontSize = 17.0f; for (FormPage page : form.getPages()) { PDPage pag = doc.getPage(i); PDRectangle pageSize = pag.getMediaBox(); try (PDPageContentStream contentStream = new PDPageContentStream(doc, pag, PDPageContentStream.AppendMode.APPEND, false)) { contentStream.setFont(f, fontSize); contentStream.beginText(); contentStream.setNonStrokingColor(Color.black); for (Pole p : page.getPola()) { int x = p.getPozycjaX(); if (p.isRightAlign()) { x -= f.getStringWidth(p.getWartosc()) / 1000 * fontSize; } contentStream .setTextMatrix(Matrix.getTranslateInstance(x, pageSize.getHeight() - p.getPozycjaY())); contentStream.showText(p.getWartosc()); } contentStream.endText(); } i++; } }
From source file:com.util.PDFBoxTools.java
/** * This sets the line breaks for PDFBox. PDFBox does not know when lines or * pages end so this method configures the best possible spacing and breaks * the lines where it deems it necessary * * @param origText String//w ww. j a va 2 s . c om * @param width float * @param fontSize float * @param pdfFont PDFont * @return List of Strings for each line of text to loop through. */ public static List<String> setLineBreaks(String origText, float width, float fontSize, PDFont pdfFont) { List<String> lines = new ArrayList<>(); origText = (origText == null ? "" : " " + origText); origText = Normalizer.normalize(origText, Normalizer.Form.NFD); origText = origText.replaceAll("[^\\n\\r\\t\\p{Print}]", ""); //remove htmllinks origText = origText.replaceAll("\\<http.*?\\>", ""); //strip ZERO WIDTH SPACE, strip CHARACTER TABULATION, strip controlLF origText = origText.replaceAll("[\\u200B\\u0009\\u000A]", ""); //strip controlCR origText = origText.replaceAll("\\u000D", System.lineSeparator()); origText = origText.replaceAll("[^\\x00-\\x7F]", ""); origText = origText.replaceAll(System.lineSeparator(), System.lineSeparator() + " "); String[] splitText = origText.split(System.lineSeparator()); for (String text : splitText) { int lastSpace = -1; while (text.length() > 0) { try { int spaceIndex = text.indexOf(' ', lastSpace + 1); if (spaceIndex < 0) { spaceIndex = text.length(); } String subString = text.substring(0, spaceIndex); float size = fontSize * pdfFont.getStringWidth(subString) / 1000; // System.out.printf("'%s' - %f of %f\n", subString, size, width); if (size > width) { if (lastSpace < 0) { lastSpace = spaceIndex; } subString = text.substring(0, lastSpace); lines.add(subString); text = text.substring(lastSpace).trim(); // System.out.printf("'%s' is line\n", subString); lastSpace = -1; } else if (spaceIndex == text.length()) { lines.add(text); // System.out.printf("'%s' is line\n", text); text = ""; } else { lastSpace = spaceIndex; } } catch (IOException ex) { ExceptionHandler.Handle(ex); } } } return lines; }
From source file:de.hrogge.CompactPDFExport.PDFSeite.java
License:Apache License
public float berechneTextUeberlauf(PDFont font, int x1, int x2, int height, String text) throws IOException { PDFontDescriptor descr = font.getFontDescriptor(); float boxProp, textProp; float boxWidth, boxHeight; float textWidth, textHeight; boxHeight = (pageHeight / 60.0f * height) - 2 * randText; boxWidth = (getX(x2) - getX(x1)) - 2 * randText; boxProp = boxWidth / boxHeight;/* www .ja v a 2s . c om*/ textHeight = (descr.getFontBoundingBox().getHeight()) / 1000f; textWidth = font.getStringWidth(text) / 1000f; textProp = textWidth / textHeight; if (textProp > boxProp) { return textProp / boxProp; } return 1.0f; }
From source file:de.hrogge.CompactPDFExport.PDFSeite.java
License:Apache License
public void drawText(PDFont font, int x1, int x2, int y1, int y2, String text, boolean center) throws IOException { PDFontDescriptor descr = font.getFontDescriptor(); float boxProp, textProp; float boxWidth, boxHeight; float textWidth, textHeight; float shiftX, shiftY; boxHeight = (getY(y1) - getY(y2)) - 2 * randText; boxWidth = (getX(x2) - getX(x1)) - 2 * randText; boxProp = boxWidth / boxHeight;/*from w ww .ja va 2 s .c o m*/ textHeight = (descr.getFontBoundingBox().getHeight()) / 1000f; textWidth = font.getStringWidth(text) / 1000f; textProp = textWidth / textHeight; /* begrenze vertikale Skalierung */ while (textProp / boxProp > 1.75 && text.contains(" ")) { text = text.substring(0, text.lastIndexOf(' ')) + "..."; textWidth = font.getStringWidth(text) / 1000f; textProp = textWidth / textHeight; } stream.beginText(); if (textProp > boxProp) { /* scale over width */ stream.setFont(font, boxWidth / textWidth); shiftX = 0f; shiftY = boxHeight / 2 - (textHeight * boxWidth / textWidth) / 2; shiftY += -descr.getDescent() / 1000 * (boxWidth / textWidth); } else { /* scale over height */ stream.setFont(font, boxHeight / textHeight); if (center) { shiftX = boxWidth / 2 - textWidth * (boxHeight / textHeight) / 2; } else { shiftX = 0f; } shiftY = -descr.getDescent() / 1000 * (boxHeight / textHeight); } stream.moveTextPositionByAmount(getX(x1) + randText + shiftX, getY(y2) + randText + shiftY); stream.drawString(text); stream.endText(); }
From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer.java
License:Apache License
private void showTextOnStream(IFontTextDrawerEnv env, PDPageContentStream contentStream, Font attributeFont, PDFont font, boolean isStrikeThrough, boolean isUnderline, boolean isLigatures, String text) throws IOException { if (isStrikeThrough || isUnderline) { // noinspection unused float stringWidth = font.getStringWidth(text); // noinspection unused LineMetrics lineMetrics = attributeFont.getLineMetrics(text, env.getFontRenderContext()); /*//ww w .j a v a 2 s .c om * TODO: We can not draw that yet, we must do that later. While in textmode its * not possible to draw lines... */ } // noinspection StatementWithEmptyBody if (isLigatures) { /* * No idea how to map this ... */ } contentStream.showText(text); }
From source file:de.thb.ue.backend.util.PDFGeneration.java
License:Apache License
public static void createQRCPDF(List<BufferedImage> tickets, String evaluationUID, String subject, SemesterType semesterType, int year, File workingDirectory) { PDDocument document = new PDDocument(); PDPage page = new PDPage(); // PDRectangle rectangle = new PDRectangle(page.findCropBox().getWidth(), 500); // page.setCropBox(rectangle); document.addPage(page);//w w w.java 2 s . co m PDXObjectImage pdImage; try { PDPageContentStream contentStream = new PDPageContentStream(document, page); int x = 0; // start from top of page instead of bottom // somehow the first row gets swallowed -> start with second row instead int y = (int) page.findCropBox().getHeight() - (new PDJpeg(document, tickets.get(0))).getHeight(); int imageHeight; int imageWidth; String qrCodePurpose = "Evaluation QR-Codes fr das Fach " + subject + " im " + (semesterType == SemesterType.WINTER ? "Winter" : "Sommer") + "semester " + Integer.toString(year); PDFont font = PDType1Font.HELVETICA; int fontSize = 13; // center text horizontally float textWidth = font.getStringWidth(qrCodePurpose) / 1000 * fontSize; int textX = (int) ((page.findCropBox().getWidth() / 2) - (textWidth / 2)); // add QR-Codes for (int i = 0; i < tickets.size(); i++) { pdImage = new PDJpeg(document, tickets.get(i)); imageHeight = pdImage.getHeight(); imageWidth = pdImage.getWidth(); if ((imageWidth + x) > page.findCropBox().getWidth()) { x = 0; y -= imageHeight; } if (y <= 0) { writeText(contentStream, page, qrCodePurpose, imageHeight, textX, (y + imageHeight) - (imageHeight / 4), fontSize, font); page = new PDPage(); document.addPage(page); contentStream.close(); contentStream = new PDPageContentStream(document, page); y = (int) page.findCropBox().getHeight() - pdImage.getHeight(); x = 0; } contentStream.drawImage(pdImage, x, y); if (i + 1 >= tickets.size()) { writeText(contentStream, page, qrCodePurpose, imageHeight, textX, y - 20, fontSize, font); } x += imageWidth; } /*PDDocumentCatalog catalog = document.getDocumentCatalog(); catalog.getAcroForm().setXFA(null);*/ contentStream.close(); if (!workingDirectory.exists()) { workingDirectory.mkdir(); } File outputFile = new File(workingDirectory, "qrcodes.pdf"); document.save(outputFile); } catch (IOException | COSVisitorException e) { e.printStackTrace(); } finally { try { document.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:dpfmanager.conformancechecker.tiff.reporting.PdfReport.java
License:Open Source License
private float getSize(int font_size, PDFont font, String text) throws IOException { return font_size * font.getStringWidth(text) / 1000; }
From source file:fi.nls.oskari.printout.printing.pdfbox.UsingTextMatrix.java
License:Apache License
/** * creates a sample document with some text using a text matrix. * /*from ww w .j a va 2 s .c o m*/ * @param message * The message to write in the file. * @param outfile * The resulting PDF. * * @throws IOException * If there is an error writing the data. * @throws COSVisitorException * If there is an error writing the PDF. */ public void doIt(String message, String outfile) throws IOException, COSVisitorException { // the document PDDocument doc = null; try { doc = new PDDocument(); // Page 1 PDFont font = PDType1Font.HELVETICA; PDPage page = new PDPage(); page.setMediaBox(PDPage.PAGE_SIZE_A4); doc.addPage(page); float fontSize = 12.0f; PDRectangle pageSize = page.findMediaBox(); System.err.println("pageSize " + pageSize); System.err.println( "pageSize cm " + pageSize.getWidth() / 72 * 2.54 + "," + pageSize.getHeight() / 72 * 2.54); float centeredXPosition = (pageSize.getWidth() - fontSize / 1000f) / 2f; float stringWidth = font.getStringWidth(message); float centeredYPosition = (pageSize.getHeight() - (stringWidth * fontSize) / 1000f) / 3f; PDPageContentStream contentStream = new PDPageContentStream(doc, page, false, false); contentStream.setFont(font, fontSize); contentStream.beginText(); // counterclockwise rotation for (int i = 0; i < 8; i++) { contentStream.setTextRotation(i * Math.PI * 0.25, centeredXPosition, pageSize.getHeight() - centeredYPosition); contentStream.drawString(message + " " + i); } // clockwise rotation for (int i = 0; i < 8; i++) { contentStream.setTextRotation(-i * Math.PI * 0.25, centeredXPosition, centeredYPosition); contentStream.drawString(message + " " + i); } contentStream.endText(); contentStream.close(); // Page 2 page = new PDPage(); page.setMediaBox(PDPage.PAGE_SIZE_A4); doc.addPage(page); fontSize = 1.0f; contentStream = new PDPageContentStream(doc, page, false, false); contentStream.setFont(font, fontSize); contentStream.beginText(); // text scaling for (int i = 0; i < 10; i++) { contentStream.setTextScaling(12 + (i * 6), 12 + (i * 6), 100, 100 + i * 50); contentStream.drawString(message + " " + i); } contentStream.endText(); contentStream.close(); // Page 3 page = new PDPage(); page.setMediaBox(PDPage.PAGE_SIZE_A4); doc.addPage(page); fontSize = 1.0f; contentStream = new PDPageContentStream(doc, page, false, false); contentStream.setFont(font, fontSize); contentStream.beginText(); int i = 0; // text scaling combined with rotation contentStream.setTextMatrix(12, 0, 0, 12, centeredXPosition, centeredYPosition * 1.5); contentStream.drawString(message + " " + i++); contentStream.setTextMatrix(0, 18, -18, 0, centeredXPosition, centeredYPosition * 1.5); contentStream.drawString(message + " " + i++); contentStream.setTextMatrix(-24, 0, 0, -24, centeredXPosition, centeredYPosition * 1.5); contentStream.drawString(message + " " + i++); contentStream.setTextMatrix(0, -30, 30, 0, centeredXPosition, centeredYPosition * 1.5); contentStream.drawString(message + " " + i++); contentStream.endText(); contentStream.close(); // Page 4 { page = new PDPage(); page.setMediaBox(PDPage.PAGE_SIZE_A4); doc.addPage(page); fontSize = 1.0f; contentStream = new PDPageContentStream(doc, page, false, false); contentStream.setFont(font, fontSize); contentStream.beginText(); AffineTransform root = new AffineTransform(); root.scale(72.0 / 2.54, 72.0 / 2.54); for (i = 0; i < pageSize.getHeight() / 72 * 2.54; i++) { // text scaling combined with rotation { AffineTransform rowMatrix = new AffineTransform(root); rowMatrix.translate(1, i); contentStream.setTextMatrix(rowMatrix); contentStream.drawString(message + " " + i); } } contentStream.endText(); contentStream.close(); } doc.save(outfile); } finally { if (doc != null) { doc.close(); } } }
From source file:info.informationsea.venn.graphics.VennDrawPDF.java
License:Open Source License
public static Rectangle2D stringBoundingBox(PDFont font, String str, int fontSize) { try {// w w w .ja v a 2 s. c om return new Rectangle2D.Double(0, 0, font.getStringWidth(str) / 1000 * fontSize, fontSize); } catch (IOException e) { throw new RuntimeException("Cannot calculate font width"); } }
From source file:jgnash.report.pdf.Report.java
License:Open Source License
private static float getStringWidth(final String text, final PDFont font, final float fontSize) throws IOException { return (float) Math.ceil(font.getStringWidth(text) / 1000f * fontSize); }