Example_27

Drawing Thai, Hebrew, Arabic and Persian text, including right to left text blocks, in a PDF/UA document.

Example_27.pdf — the PDF this example creates.

/*
 * Example_27.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_27.java
 */
public class Example_27 {
    public Example_27() throws Exception {
        PDF pdf = new PDF(
                new BufferedOutputStream(
                        new FileOutputStream("Example_27.pdf")));
        pdf.setCompliance(Compliance.PDF_UA_1);
        pdf.setTitle("Thai, Hebrew, Arabic and Persian text");

        // Thai font, whose stream keeps the marks of its GPOS table, which
        // places the tone marks above the upper vowels.
        // Font f1 = new Font(pdf, "fonts/NotoSansThai/NotoSansThai-Regular.ttf.stream");
        Font f1 = new Font(pdf, IBMPlexSansThai.Regular);
        f1.setSize(12f);

        // Hebrew font
        // Font f2 = new Font(pdf, "fonts/NotoSansHebrew/NotoSansHebrew-Regular.ttf.stream");
        Font f2 = new Font(pdf, IBMPlexSansHebrew.Regular);
        f2.setSize(12f);

        // Arabic font
        // Font f3 = new Font(pdf, "fonts/NotoSansArabic/NotoSansArabic-Regular.ttf.stream");
        Font f3 = new Font(pdf, IBMPlexSansArabic.Regular);
        f3.setSize(12f);

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

        TextBlock textBlock = new TextBlock(f1, Content.ofTextFile("data/languages/thai.txt"));
        textBlock.setLocation(30f, 30f);
        textBlock.setWidth(430f);
        textBlock.setBorderColor(Color.blue);
        textBlock.setPadding(10f);
        textBlock.setLanguage("th");
        float[] xy = textBlock.drawOn(page);

        float x = 570f;
        float y = xy[1] + 55f;

        String str = "כך נראית תחתית הטבלה עם סיום הפלייאוף התחתון:";
        str = Bidi.reorderVisually(str);
        TextLine textLine = new TextLine(f2, str);
        textLine.setLanguage("he");
        textLine.setLocation(x - f2.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = "10. הפועל כפר סבא 38 נקודות (הפרש שערים 14-)";
        str = Bidi.reorderVisually(str);
        textLine = new TextLine(f2, str);
        textLine.setLanguage("he");
        textLine.setLocation(x - f2.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = "11. הפועל קריית שמונה 36 נקודות (הפרש שערים 7-)";
        str = Bidi.reorderVisually(str);
        textLine = new TextLine(f2, str);
        textLine.setLanguage("he");
        textLine.setLocation(x - f2.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = "12. הפועל חיפה 34 נקודות (הפרש שערים 10-)";
        str = Bidi.reorderVisually(str);
        textLine = new TextLine(f2, str);
        textLine.setLanguage("he");
        textLine.setLocation(x - f2.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = "13. הפועל עכו 34 נקודות (הפרש שערים 21-)";
        str = Bidi.reorderVisually(str);
        textLine = new TextLine(f2, str);
        textLine.setLanguage("he");
        textLine.setLocation(x - f2.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        y += 65f;
        str = Bidi.reorderVisually(
                "قالت شركة PSA بيجو ستروين الفرنسية وشريكتها الصينية شركة دونغفينغ موترز الاربعاء إنهما اتفقتا");
        textLine = new TextLine(f3, str);
        textLine.setLanguage("ar");
        textLine.setLocation(x - f3.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = Bidi.reorderVisually(
                "على التعاون في تطوير السيارات التي تعمل بالطاقة الكهربائية اعتبارا من عام 2019.");
        textLine = new TextLine(f3, str);
        textLine.setLanguage("ar");
        textLine.setLocation(x - f3.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = Bidi.reorderVisually(
                "وجاء في تصريح اصدرته في باريس الشركة الفرنسية ان الشركتين ستنتجان نموذجا كهربائيا مشتركا تستخدمه كل");
        textLine = new TextLine(f3, str);
        textLine.setLanguage("ar");
        textLine.setLocation(x - f3.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = Bidi.reorderVisually(
                "من بيجو وسيتروين ودونغفينغ.");
        textLine = new TextLine(f3, str);
        textLine.setLanguage("ar");
        textLine.setLocation(x - f3.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        str = Bidi.reorderVisually(
                "وقالت إن الخطة تهدف الى تحقيق عائد يزيد على 100 مليار يوان (15,4 مليار دولار) بحلول عام 2020.");
        textLine = new TextLine(f3, str);
        textLine.setLanguage("ar");
        textLine.setLocation(x - f3.stringWidth(str), y += 20f);
        textLine.drawOn(page);

        // Right to left text in text blocks, wrapped at their width.
        page = new Page(pdf, Letter.PORTRAIT);

        textBlock = new TextBlock(f2, Content.ofTextFile("data/languages/hebrew.txt"));
        textBlock.setLocation(180f, 30f);
        textBlock.setWidth(400f);
        textBlock.setBorderColor(Color.blue);
        textBlock.setPadding(10f);
        textBlock.setRightToLeft(true);
        textBlock.setLanguage("he");
        xy = textBlock.drawOn(page);

        textBlock = new TextBlock(f3, Content.ofTextFile("data/languages/arabic.txt"));
        textBlock.setLocation(180f, xy[1] + 30f);
        textBlock.setWidth(400f);
        textBlock.setBorderColor(Color.blue);
        textBlock.setPadding(10f);
        textBlock.setRightToLeft(true);
        textBlock.setLanguage("ar");
        xy = textBlock.drawOn(page);

        textBlock = new TextBlock(f3, Content.ofTextFile("data/languages/persian.txt"));
        textBlock.setLocation(180f, xy[1] + 30f);
        textBlock.setWidth(400f);
        textBlock.setBorderColor(Color.blue);
        textBlock.setPadding(10f);
        textBlock.setRightToLeft(true);
        textBlock.setLanguage("fa");
        textBlock.drawOn(page);

        pdf.complete();
    }

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

©2026 PDFjet Software.