Change direction of written text for Hebrew letter

2019-07-13 11:33发布

问题:

When I write Hebrew letters on a PDF they appear from left to right.

How can I change the direction?

I am using Paragraph.

回答1:

Have a look at this example :

Document document = new Document(PageSize.A4);
String filename = ""; // Set the relative path and name for the output file
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
// Fix the path to the font if needed
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, true);
Font font = new Font(bf, 14);
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(36, 770, 569, 36);
column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
String text = "הטקסט שלך בעברית"; // Your text in hebrew
column.addElement(new Paragraph(text, font));
column.go();
document.close();


回答2:

I've made some simple functions to handle this issue. It just flips the text to the other side and returns it as a phrase that you can add to the document. The problem with columns is that you need precise information of the place you are going to write on. Also I advice to use PdfContentByte to write at a more precise place.

public Phrase makingPhrases(string toPhrase, bool toReverse,int SF)
{

    BaseFont unicode = BaseFont.CreateFont(Server.MapPath("font/mriam.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font bold = new Font(unicode, SF);
    if (toReverse)
        return new Phrase(revertText(toPhrase), bold);
    else
        return new Phrase(toPhrase, bold);
}
public string revertText(string revertTo)
{
    string toret = "";
    for (int i = 0; i < revertTo.Length; i++)
    {
        toret += revertTo[revertTo.Length - i - 1];
    }
    return toret.ToString();
}


标签: java itext