[英]How to change direction of Hebrew letters?
我正在使用itext 7.1.8
,我需要在我的文檔中保存希伯來語文本。 我在這里找到了這個解決方案,但它對我不起作用。
我的代碼如下所示:
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();
}
}
為什么這段代碼不起作用? 如何設置文本方向?
請查看此頁面https://kb.itextpdf.com/home/it5kb/faq/how-to-set-rtl-direction-for-hebrew-when-converting-html-to-pdf 。 它描述了您遇到的相同問題。 我認為主要的技巧在於文本字體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.