0

I'm using itext 7.1.8 and I need to save Hebrew text in my document. I found this solution here but it doesn't work for me.
My code looks like the following:

public class RunItextApp {
    public static void main(String[] args) throws Exception {
        final String filename = "simple.pdf";
        final String hebrew = "שדג";
        final String text = "\u05E9\u05D3\u05D2";

        createSimplePdf(filename, hebrew);
    }

    private static void createSimplePdf(String filename, String text) throws Exception {

        final String path = RunItextApp.class.getResource("/Arial.ttf").getPath();
        final PdfFont font = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H, true);
        Style hebrewStyle = new Style()
                .setBaseDirection(BaseDirection.RIGHT_TO_LEFT)
                .setTextAlignment(TextAlignment.RIGHT)
                .setFontSize(14)
                .setFont(font);

        final PdfWriter pdfWriter = new PdfWriter(filename);
        final PdfDocument pdfDocument = new PdfDocument(pdfWriter);
        final Document pdf = new Document(pdfDocument);
        pdf.setBaseDirection(BaseDirection.RIGHT_TO_LEFT);
        pdf.add(
                new Paragraph(text)
                        .setFontScript(Character.UnicodeScript.HEBREW)
                        .addStyle(hebrewStyle)
        );
        pdf.close();
    }
}

Why this code doesn't work? How can I set text direction?

John
  • 1,375
  • 4
  • 17
  • 40
  • Hey John. For Hebrew support, you would require pdfCalligraph - https://itextpdf.com/en/products/itext-7/pdfcalligraph – André Lemos Jun 05 '20 at 13:18
  • @AndréLemos but why ```setBaseDirection``` doesn't work? – John Jun 09 '20 at 12:11
  • as per the API (https://api.itextpdf.com/iText7/java/7.1.11/com/itextpdf/layout/ElementPropertyContainer.html#setBaseDirection-com.itextpdf.layout.property.BaseDirection-) that method applies to _neutral_ _text_. For Hebrew, the directionality is defined, and pdfCalligraph picks up on that information. – André Lemos Jun 09 '20 at 13:15
  • 1
    @AndréLemos Can you tell me how I can change a text direction manually(forcibly) without using ```pdfCaligraph```. – John Jun 09 '20 at 13:54
  • you mean, writing \u05D2\u05D3\u05E9 ? – André Lemos Jun 10 '20 at 08:09
  • 1
    @AndréLemos I mean, if I know that is a Hebrew and I need to write it right to left direction, can I set up a text direction (RTL) manually? – John Jun 10 '20 at 08:39
  • you mean, you want to detect the text language script type, and invert the order that the characters are rendered? – André Lemos Jun 10 '20 at 10:49

1 Answers1

-1

Please take a look at this page https://kb.itextpdf.com/home/it5kb/faq/how-to-set-rtl-direction-for-hebrew-when-converting-html-to-pdf. It is described the same problem you have. I think that the main trick is in text font.

Stefan
  • 1
  • 1
  • 3
  • The iText Knowledge Base article you linked is about setting the direction *when converting HTML to PDF* in *iText 5*. The question here is about setting the direction *when creating `Paragraph` objects manually* in *iText 7*. Furthermore, link-only answers usually are not very welcome on stack overflow. – mkl Jun 05 '20 at 14:35