Example_23

Example showing the location, ascent, descent and end point of text drawn with the TextBlock component.

Example_23.pdf — the PDF this example creates.

/*
 * Example_23.java
 *
 * Copyright (c) 2026 PDFjet Software
 * Licensed under the MIT License. See LICENSE file in the project root.
 */
package examples;

import java.io.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;

/**
 * Example_23.java
 */
public class Example_23 {
    public Example_23() throws Exception {
        PDF pdf = new PDF(new FileOutputStream("Example_23.pdf"));

        Font f1 = new Font(pdf, IBMPlexSans.Regular);
        f1.setSize(72f);

        Font f2 = new Font(pdf, IBMPlexSans.Regular);
        f2.setSize(24f);

        Page page = new Page(pdf, Letter.PORTRAIT);

        float x1 = 90f;
        float y1 = 50f;

        TextLine textLine = new TextLine(f2, "(x1, y1)");
        textLine.setLocation(x1, y1 - 15f);
        textLine.drawOn(page);

        TextBlock textBlock = new TextBlock(f1,
            "Hello, World! This example shows the functionality of the TextBlock.");
        textBlock.setLocation(x1, y1);
        textBlock.setWidth(500f);
        textBlock.setBorderColor(Color.lightgreen);
        textBlock.setBackgroundColor(Color.lightgreen);
        textBlock.setTextColor(Color.black);
        float[] xy = textBlock.drawOn(page);

        // Text on the left
        TextLine ascentText = new TextLine(f2, "Ascent");
        ascentText.setFontSize(18f);
        ascentText.setLocation(x1 - 85f, y1 + 40f);
        ascentText.drawOn(page);

        TextLine descentText = new TextLine(f2, "Descent");
        descentText.setFontSize(18f);
        descentText.setLocation(x1 - 85f, y1 + f1.getAscent() + 15f);
        descentText.drawOn(page);

        // Line beside the text ascent
        Line blueLine = new Line(
            x1 - 10f,
            y1,
            x1 - 10f,
            y1 + f1.getAscent());
        blueLine.setStrokeColor(Color.blue);
        blueLine.setStrokeWidth(3f);
        blueLine.drawOn(page);

        // Line beside the text descent
        Line redLine = new Line(
            x1 - 10f,
            y1 + f1.getAscent(),
            x1 - 10f,
            y1 + f1.getAscent() + f1.getDescent());
        redLine.setStrokeColor(Color.red);
        redLine.setStrokeWidth(3f);
        redLine.drawOn(page);

        Line baseLine = new Line(
                x1,
                y1 + f1.getAscent(),
                xy[0],
                y1 + f1.getAscent());
        baseLine.drawOn(page);

        Line descentLine = new Line(
                x1,
                y1 + (f1.getAscent() + f1.getDescent()),
                xy[0],
                y1 + (f1.getAscent() + f1.getDescent()));
        descentLine.drawOn(page);

        Line ascentLine = new Line(
                x1,
                y1 + f1.getBodyHeight() + f1.getAscent(),
                xy[0],
                y1 + f1.getBodyHeight() + f1.getAscent());
        ascentLine.drawOn(page);

        Point p1 = new Point(x1, y1);
        p1.setRadius(5f);
        p1.drawOn(page);

        Point p2 = new Point(xy[0], xy[1]);
        p2.setRadius(5f);
        p2.drawOn(page);

        f2.setSize(24f);
        TextLine textLine3 = new TextLine(f2, "(x2, y2)");
        textLine3.setLocation(xy[0] - 80f, xy[1] + 30f);
        textLine3.drawOn(page);

        Rect rect = new Rect(xy[0], xy[1], 20f, 20f);
        rect.setBorderColor(Color.black);
        rect.drawOn(page);

        pdf.complete();
    }

    public static void main(String[] args) throws Exception {
        long time0 = System.currentTimeMillis();
        new Example_23();
        long time1 = System.currentTimeMillis();
        System.out.printf("Example_23 => %4d ms%n", time1 - time0);
    }
}   // End of Example_23.java

©2026 PDFjet Software.