2012-08-29 63 views
2

當我在PDF上書寫希伯來語字母時,他們從左到右出現。更改希伯來語字母書寫文字的方向iText

我該如何改變方向?我正在使用Paragraph

+1

的可能重複的[java的iText的創建與希伯來語(RTL)PDF和英文](http://stackoverflow.com/questions/5855078/java-itext-create-pdf-with-hebrew-rtl-and -english) –

+0

你是否從html或「手動」創建pdf? – iddqd

+0

@iddqd「手動」 –

回答

1

看一看這個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(); 
+0

我有你的代碼的一些錯誤,這工作column.RunDirection = PdfWriter.RUN_DIRECTION_RTL; String text =「הטקסטשלךבעברית」; //你的希伯來語文字 column.AddElement(new Paragraph(text,Gisha)); column.Go(); – Harry

0

我做了一些簡單的函數來處理這個問題。它只是將文本翻轉到另一邊,並將其作爲可添加到文檔中的短語返回。 與列的問題是,您需要準確的信息,你要寫的地方。另外我建議使用PdfContentByte在更精確的地方寫。

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(); 
}