Reversing Strings in Right To Left (BiDirectional) Languages in iTextSharp

假如想象 提交于 2019-12-01 18:20:30

To show RTL texts by using iTextSharp correctly:

  • You need to set the font's encoding to BaseFont.IDENTITY_H
  • Then you have to use container elements which support RunDirection, such as PdfPCell, ColumnText, etc. and now you can set their element.RunDirection = PdfWriter.RUN_DIRECTION_RTL;

PDFCreator is a free tool to create PDF files from nearly any Windows application. It installs as a Windows printer driver, such that it can be used by any Windows program that has a print functionality.

You can treat your input as simple text strings to be printed, and maybe using the print menu option of Notepad will create the correct PDF.

If you want to dive a little deeper into right to left C# printing, use StringFormatFlags.DirectionRightToLeft string format with Graphics.DrawString() calls.

A snippet from a PrintPage Event Handler:

lineFmt = new StringFormat(StringFormatFlags.DirectionRightToLeft);
e.Graphics.DrawString(textToPrint, font, Brushes.Black, startX, ypos, lineFmt);
shmulik

Just put the string in table cell:

PdfPCell cell1 = new PdfPCell(new Phrase("מספר",font));
cell1.HorizontalAlignment = 2; //0=Left, 1=Centre, 2=Right

HTML displays Hebrew/Arabic in Logical mode, and in PDF you need to store it Visual mode. What you need to do is convert from Logical to Visual mode. There are some libraries which do this (google for minibidi which is BSD licensed IMHO, or fribidi which is GPL or LGPL).

My real suggestion would be to change direction. Write a very small application in Qt4 which takes as first argument the URL, and the second the PDF to write. Since Qt4 has HTML support (via QtWebKit) has has the option to print to PDF (post script and SVG as well) this should be simpler then writing your own HTML->PDF solution.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!