Refine search
float yScalingFactor = FONTSCALE * font.getFontMatrix().getScaleY(); float xScalingFactor = FONTSCALE * font.getFontMatrix().getScaleX(); float width = font.getStringWidth(value) * font.getFontMatrix().getScaleX(); float widthBasedFontSize = contentRect.getWidth() / width * xScalingFactor; float height = (font.getFontDescriptor().getCapHeight() + -font.getFontDescriptor().getDescent()) * font.getFontMatrix().getScaleY(); if (height <= 0) height = font.getBoundingBox().getHeight() * font.getFontMatrix().getScaleY(); float heightBasedFontSize = contentRect.getHeight() / height * yScalingFactor;
/** * This methods extracts from the Font dictionary all mandatory fields. If a mandatory field is missing, the list of * ValidationError in the FontContainer is updated. */ protected void checkMandatoryFields() { COSDictionary fontDictionary = font.getCOSObject(); boolean areFieldsPResent = fontDictionary.containsKey(COSName.TYPE); areFieldsPResent &= fontDictionary.containsKey(COSName.SUBTYPE); areFieldsPResent &= fontDictionary.containsKey(COSName.BASE_FONT); areFieldsPResent &= fontDictionary.containsKey(COSName.DESCENDANT_FONTS); areFieldsPResent &= fontDictionary.containsKey(COSName.ENCODING); if (!areFieldsPResent) { this.fontContainer.push(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, font.getName() + ": Some keys are missing from composite font dictionary")); } }
AffineTransform at = textRenderingMatrix.createAffineTransform(); at.concatenate(font.getFontMatrix().createAffineTransform()); if (!font.isEmbedded() && !font.isVertical() && !font.isStandard14() && font.hasExplicitWidth(code)) float fontWidth = font.getWidthFromFont(code); if (fontWidth > 0 && // ignore spaces Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001)
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();
/** * Returns the width of the given Unicode string. * * @param text The text to get the width of. * @return The width of the string in 1/1000 units of text space. * @throws IOException If there is an error getting the width information. */ public float getStringWidth(String text) throws IOException { byte[] bytes = encode(text); ByteArrayInputStream in = new ByteArrayInputStream(bytes); float width = 0; while (in.available() > 0) { int code = readCode(in); width += getWidth(code); } return width; }
/** * Returns true if this font is one of the "Standard 14" fonts and receives special handling. */ public boolean isStandard14() { // this logic is based on Acrobat's behaviour, see PDFBOX-2372 // embedded fonts never get special treatment if (isEmbedded()) { return false; } // if the name matches, this is a Standard 14 font return Standard14Fonts.containsName(getName()); }
Matrix textMatrix = getTextMatrix(); BoundingBox bbox = font.getBoundingBox(); if (bbox.getLowerLeftY() < Short.MIN_VALUE) PDFontDescriptor fontDescriptor = font.getFontDescriptor(); if (fontDescriptor != null) if (font instanceof PDType3Font) height = font.getFontMatrix().transformPoint(0, glyphHeight).y; if (font.isVertical()) displacementX = font.getWidth(code) / 1000; Matrix td = Matrix.getTranslateInstance(tx, ty); if (font instanceof PDType3Font) glyphSpaceToTextSpaceFactor = font.getFontMatrix().getScaleX(); spaceWidthText = font.getSpaceWidth() * glyphSpaceToTextSpaceFactor; spaceWidthText = font.getAverageFontWidth() * glyphSpaceToTextSpaceFactor; unicode = font.toUnicode(code, glyphList);
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(); } } }
borderULine.setWidth(INCH / 72); // 1 point float pw = page1.getMediaBox().getUpperRightX(); float ph = page1.getMediaBox().getUpperRightY(); try (PDPageContentStream contents = new PDPageContentStream(document, page1)) contents.beginText(); contents.setFont(font, 18); contents.newLineAtOffset(INCH, ph - INCH - 18); contents.showText("PDFBox"); float textWidth = font.getStringWidth("PDFBox") / 1000 * 18; PDRectangle position = new PDRectangle(); position.setLowerLeftX(INCH); position.setLowerLeftY(ph - INCH - 18); textWidth = font.getStringWidth("External URL") / 1000 * 18; position = new PDRectangle(); position.setLowerLeftX(INCH); textWidth = font.getStringWidth("Jump to page three") / 1000 * 18; position = new PDRectangle(); position.setLowerLeftX(INCH);
float width = mediabox.getWidth() - 2*margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; 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) contentStream.beginText(); contentStream.setFont(pdfFont, fontSize); contentStream.newLineAtOffset(startX, startY); for (String line: lines)
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, AppendMode.OVERWRITE, false); contentStream.setFont( font, fontSize ); contentStream.beginText(); contentStream.setTextMatrix(Matrix.getRotateInstance(i * Math.PI * 0.25, centeredXPosition, pageSize.getHeight() - centeredYPosition)); contentStream.showText(message + " " + i); contentStream.setTextMatrix(Matrix.getRotateInstance(-i*Math.PI*0.25, centeredXPosition, centeredYPosition)); contentStream.showText(message + " " + i); contentStream.setTextMatrix(new Matrix(12 + (i * 6), 0, 0, 12+(i*6), 100, 100+i*50)); contentStream.showText(message + " " + i);
float width = mediabox.getWidth() - 2*margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; 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) contentStream.beginText(); contentStream.setFont(pdfFont, fontSize); contentStream.moveTextPositionByAmount(startX, startY); for (String line: lines)
float stringWidth = font.getStringWidth(message) * FONT_SIZE; float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * FONT_SIZE; try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false)) contentStream.beginText(); contentStream.setFont(font, FONT_SIZE); contentStream.setTextMatrix( Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f)); contentStream.setTextMatrix( Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f * 2)); contentStream.showTextWithPositioning(text.toArray()); contentStream.setTextMatrix(Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f * 3));
float stringWidth = font.getStringWidth( message )*fontSize/1000f; float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth(); float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight(); float centerX = rotate ? pageHeight/2f : (pageWidth - stringWidth)/2f; float centerY = rotate ? (pageWidth - stringWidth)/2f : pageHeight/2f; try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true)) contentStream.beginText(); contentStream.setFont( font, fontSize ); contentStream.setNonStrokingColor(255, 0, 0); contentStream.setTextMatrix(Matrix.getRotateInstance(Math.PI / 2, centerX, centerY)); contentStream.setTextMatrix(Matrix.getTranslateInstance(centerX, centerY)); contentStream.showText(message);
doc.addPage(page); PDRectangle pageSize = page.getMediaBox(); float pageWidth = pageSize.getWidth(); float fontSize = 12; float stringWidth = font.getStringWidth( message )*fontSize/1000f; float startX = 100; float startY = 100; try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false)) contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0)); contentStream.setFont( font, fontSize ); contentStream.beginText(); contentStream.newLineAtOffset(startX, startY);
contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(), clipRect.getWidth(), clipRect.getHeight()); contents.clip(); float fontBoundingBoxAtSize = font.getBoundingBox().getHeight() * fontScaleY; float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY; float fontDescentAtSize = font.getFontDescriptor().getDescent() * fontScaleY; appearanceStyle.setLeading(font.getBoundingBox().getHeight() * fontScaleY);
private Shape calculateGlyphBounds(Matrix textRenderingMatrix, PDFont font, int code) throws IOException AffineTransform at = textRenderingMatrix.createAffineTransform(); at.concatenate(font.getFontMatrix().createAffineTransform()); if (font instanceof PDType3Font) glyphBBox.setLowerLeftX(Math.max(fontBBox.getLowerLeftX(), glyphBBox.getLowerLeftX())); glyphBBox.setLowerLeftY(Math.max(fontBBox.getLowerLeftY(), glyphBBox.getLowerLeftY())); glyphBBox.setUpperRightX(Math.min(fontBBox.getUpperRightX(), glyphBBox.getUpperRightX())); glyphBBox.setUpperRightY(Math.min(fontBBox.getUpperRightY(), glyphBBox.getUpperRightY()));
float combWidth = appearanceStream.getBBox().getWidth() / maxLen; float ascentAtFontSize = font.getFontDescriptor().getAscent() / FONTSCALE * fontSize; float baselineOffset = paddingEdge.getLowerLeftY() + (appearanceStream.getBBox().getHeight() - ascentAtFontSize)/2; float currCharWidth = font.getStringWidth(combString) / FONTSCALE * fontSize/2;
float fieldWidth = appearanceStream.getBBox().getWidth(); float stringWidth = (font.getStringWidth(value) / FONTSCALE) * fontSize; float adjustAmount = fieldWidth - stringWidth - 4; int numOptions = options.size(); float yTextPos = contentRect.getUpperRightY(); yTextPos = yTextPos - font.getFontDescriptor().getAscent() / FONTSCALE * fontSize; yTextPos = yTextPos - font.getBoundingBox().getHeight() / FONTSCALE * fontSize; contents.beginText(); contents.newLineAtOffset(contentRect.getLowerLeftX(), yTextPos); contents.showText(options.get(i));
stream.setFont( font, fontSize ); double charWidth = font.getStringWidth(text); stream.beginText(); stream.moveTextPositionByAmount(0,10); float widthLeft = pageSize.getWidth(); widthLeft *= 1000.0; //due to charWidth being x1000.