My Hebrew Text in iText is left-aligned

2019-07-11 04:59发布

问题:

I am using the following code to generate Hebrew text in iText.

It works nicely (creates a Hebrew font, and makes the text flow from right-to-left) but the text is left-aligned rather than right-aligned.

Can anyone help me make it right-aligned? Notice that I tried to make the MultiColumnText right-aligned, and the paragraph right-aligned - but to no avail!

Thanks

static Color darkBlue = new Color(0x2F, 0x36, 0x99);
BaseFont unicode = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font fontDarkBlueHebrew = new Font(unicode, 12, Font.NORMAL,darkBlue);

MultiColumnText mct=new MultiColumnText();
mct.addSimpleColumn(36, PageSize.A4.width()-36);
mct.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
mct.setAlignment(Element.ALIGN_RIGHT);

Paragraph section1a = new Paragraph("כותרת 1",fontDarkBlueHebrew );
section1a.setAlignment(Element.ALIGN_RIGHT);            
mct.addElement(section1a);

Paragraph section1b = new Paragraph("הטקסט שלע",fontDarkBlueHebrew);
section1b.setAlignment(Element.ALIGN_RIGHT);  
mct.addElement(section1b);

document.add(mct);

回答1:

This is by design. When changing the writing direction, everything is reversed: left becomes right and right becomes left. So you need Element.ALIGN_LEFT instead of Element.ALIGN_RIGHT.



标签: java pdf itext