使用itext在pdf中编写阿拉伯语 [英] Writing Arabic in pdf using itext

查看:638
本文介绍了使用itext在pdf中编写阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用itext lib生成PDF文件。
我想写阿拉伯语单词。
当我运行以下代码时,单词字符反向显示。



使用的代码:

  PdfContentByte cb = docWriter.getDirectContent(); 
BaseFont bfBold = BaseFont.createFont(assets / arial.ttf,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);


createHeadings(cb,document.leftMargin(),70,السعرالاجمالي:+ tprice +L.E。);



  private void createHeadings(PdfContentByte cb,float x,float y,String text){
cb.beginText();
cb.setFontAndSize(bfBold,10);
cb.setTextMatrix(x,y);
cb.showText(text.trim());
cb.endText();
}

此图片描述了上述代码的输出:



第一行不正确,因为使用<$ c时支持RTL和阿拉伯语连字$ C> document.add()。第二行是正确的(据我所知:我读不懂阿拉伯语),因为我使用 ColumnText



这是我使用的代码:

  public static final String FONT =resources / fonts / NotoNaskhArabic-Regular.ttf ; 
public static final String ARABIC =\\\ا \ u0644 \ u0633 \ u0639 \ u0631 \\\ا \ u0644 \ u0627 \ u062c \ u0645 \ u0627 \ u0644 \ u064a ;

public void createPdf(String dest)throws IOException,DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(dest));
document.open();
字体f = FontFactory.getFont(FONT,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
短语p =新短语(这不正确:);
p.add(new Chunk(ARABIC,f));
p.add(new Chunk(:50.00 USD));
document.add(p);

p =新词组(这是正确的:);
p.add(new Chunk(ARABIC,f));
p.add(new Phrase(:50.00));

ColumnText canvas = new ColumnText(writer.getDirectContent());
canvas.setSimpleColumn(36,750,559,780);
canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
canvas.addElement(p);
canvas.go();

document.close();
}

我使用短语,但使用段落时可以得到相同的结果(段落 extends Phrase )。请澄清这是否不能回答您的问题。考虑到StackOverflow上的大多数人都不懂阿拉伯语,所以当你提出问题时以及当你说它不起作用时你必须非常明确。由于我们不懂阿拉伯语,我们不知道它应该如何工作。


I am generating PDF file using itext lib. I want to write Arabic words. When i run the below code, The words characters are reverse displayed.

The used code :

PdfContentByte cb = docWriter.getDirectContent();
BaseFont bfBold = BaseFont.createFont("assets/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);


createHeadings(cb, document.leftMargin(), 70, "السعر الاجمالي: " + tprice + " L.E.");

. . .

private void createHeadings(PdfContentByte cb, float x, float y, String text){
        cb.beginText();
        cb.setFontAndSize(bfBold, 10);
        cb.setTextMatrix(x,y);
        cb.showText(text.trim());
        cb.endText();
    }

This image describes the output of the code above: http://i.stack.imgur.com/OLoLo.jpg

解决方案

Please take a look at the Ligatures2 example.

Aren't you forgetting this line:

cb.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

The setRunDirection() method is necessary when you want iText to write the text from right to left and create ligatures where necessary. This method also exists in the context of tables in which case you apply it to a PdfPCell object instead of to a ColumnText object.

Also, I don't understand why you use this String: "السعر الاجمالي: ". Please use the Unicode notation instead (e.g. something like "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628"), because using a String like yours can create all kinds of confusion regarding encoding and ligatures. Some editors won't use the correct encoding (changing your text into gibberish); some editors will make ligatures (which isn't what iText expects).

For instance, in your case, I don't know Arabic, so I don't know if it's "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a" or "\u064a\u0644\u0627\u0645\u062c\u0627\u0644\u0627 \u0631\u0639\u0633\u0644\u0627" because I don't know if I have to start to read at the glyph with value \u0627 or at the glyph with value \u064a. In any case: iText expects the first "character" in the String to be the first thing that is read by humans.

Please take a look at the ArabicExample example:

The first line is incorrect, because RTL nor Arabic ligatures are supported when using document.add(). The second line is correct (as far as I know: I can't read Arabic) because I used ColumnText.

This is the code I used:

public static final String FONT = "resources/fonts/NotoNaskhArabic-Regular.ttf";
public static final String ARABIC = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Phrase p = new Phrase("This is incorrect: ");
    p.add(new Chunk(ARABIC, f));
    p.add(new Chunk(": 50.00 USD"));
    document.add(p);

    p = new Phrase("This is correct: ");
    p.add(new Chunk(ARABIC, f));
    p.add(new Phrase(": 50.00"));

    ColumnText canvas = new ColumnText(writer.getDirectContent());
    canvas.setSimpleColumn(36, 750, 559, 780);
    canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
    canvas.addElement(p);
    canvas.go();

    document.close();
}

I used a Phrase, but you can expect the same result when using a Paragraph (Paragraph extends Phrase). Please clarify if this doesn't answer your question. Take into account that most people on StackOverflow don't understand Arabic, so you have to be very explicit when you ask a question and when you say "it doesn't work". As we don't know Arabic, we don't know how it is supposed to work.

这篇关于使用itext在pdf中编写阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆