String title = "This is my wonderful title!"; // Or whatever title you want. int marginTop = 30; // Or whatever margin you want. PDDocument document = new PDDocument(); PDPage page = new PDPage() PDPageStreamContent stream = new PDPageContentStream(document, page); PDFont font = PDType1Font.HELVETICA_BOLD; // Or whatever font you want. int fontSize = 16; // Or whatever font size you want. float titleWidth = font.getStringWidth(title) / 1000 * fontSize; float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText(); stream.setFont(font, fontSize); stream.moveTextPositionByAmount((page.getMediaBox().getWidth() - titleWidth) / 2, page.getMediaBox().getHeight - marginTop - titleheight); stream.drawString(title); stream.endText(); stream.close();
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)
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)
newTokenWidth = font.getStringWidth(token) / 1000 * FONT_SIZE; if (newTokenWidth + consumedWidth < workablePageWidth)
float calculateWidth(PDFont font, float fontSize) throws IOException { final float scale = fontSize/FONTSCALE; float calculatedWidth = 0f; for (Word word : words) { calculatedWidth = calculatedWidth + (Float) word.getAttributes().getIterator().getAttribute(TextAttribute.WIDTH); String text = word.getText(); if (words.indexOf(word) == words.size() -1 && Character.isWhitespace(text.charAt(text.length()-1))) { float whitespaceWidth = font.getStringWidth(text.substring(text.length()-1)) * scale; calculatedWidth = calculatedWidth - whitespaceWidth; } } return calculatedWidth; }
float calculateWidth(PDFont font, float fontSize) throws IOException { final float scale = fontSize/FONTSCALE; float calculatedWidth = 0f; for (Word word : words) { calculatedWidth = calculatedWidth + (Float) word.getAttributes().getIterator().getAttribute(TextAttribute.WIDTH); String text = word.getText(); if (words.indexOf(word) == words.size() -1 && Character.isWhitespace(text.charAt(text.length()-1))) { float whitespaceWidth = font.getStringWidth(text.substring(text.length()-1)) * scale; calculatedWidth = calculatedWidth - whitespaceWidth; } } return calculatedWidth; }
float wordWidth = font.getStringWidth(word) * scale; float whitespaceWidth = font.getStringWidth(word.substring(word.length()-1)) * scale; lineWidth = lineWidth - whitespaceWidth; textLines.add(textLine); textLine = new Line(); lineWidth = font.getStringWidth(word) * scale;
float wordWidth = font.getStringWidth(word) * scale; float whitespaceWidth = font.getStringWidth(word.substring(word.length()-1)) * scale; lineWidth = lineWidth - whitespaceWidth; textLines.add(textLine); textLine = new Line(); lineWidth = font.getStringWidth(word) * scale;
float willFitWidth = font.getStringWidth(willFit) * fontSize / 1000; float willNotFitWidth = font.getStringWidth(willNotFit) * fontSize / 1000;
float currCharWidth = font.getStringWidth(combString) / FONTSCALE * fontSize/2;
(font.getStringWidth( lineWithNextWord )/FONTSCALE) * fontSize;
float stringWidth = font.getStringWidth(message) * FONT_SIZE;
float stringWidth = (font.getStringWidth(value) / FONTSCALE) * fontSize; float adjustAmount = fieldWidth - stringWidth - 4;
private static void showPageNo(PDDocument document, PDPage page, String pageText) throws IOException { int fontSize = 10; try (PDPageContentStream contents = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, true)) { float pageWidth = page.getMediaBox().getWidth(); float pageHeight = page.getMediaBox().getHeight(); PDFont font = PDType1Font.HELVETICA; contents.setFont(font, fontSize); float textWidth = font.getStringWidth(pageText) / 1000 * fontSize; contents.beginText(); contents.newLineAtOffset(pageWidth / 2 - textWidth / 2, pageHeight - INCH / 2); contents.showText(pageText); contents.endText(); } } }
float lineWidth = appearanceStyle.getFont().getStringWidth(paragraph.getText()) * appearanceStyle.getFontSize() / FONTSCALE;
float lineWidth = appearanceStyle.getFont().getStringWidth(paragraph.getText()) * appearanceStyle.getFontSize() / FONTSCALE;
float width = font.getStringWidth(value) * font.getFontMatrix().getScaleX(); float widthBasedFontSize = contentRect.getWidth() / width * xScalingFactor;
float stringWidth = font.getStringWidth( message )*fontSize/1000f;
float pageWidth = pageSize.getWidth(); float fontSize = 12; float stringWidth = font.getStringWidth( message )*fontSize/1000f; float startX = 100; float startY = 100;
float stringWidth = font.getStringWidth( message ); float centeredYPosition = (pageSize.getHeight() - (stringWidth*fontSize)/1000f)/3f;