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

浪尽此生 提交于 2019-12-01 17:51:30

问题


I'm using iTextSharp( C# iText port) to create pdfs from text/html. Most of my text is in Hebrew, a Right-To-Left language.

My problem is that PDFs show RTL langauge in reverse, so I need to reverse my strings in a way that would only reverse the RTL text without reversing any numbers or text in English. It is my understanding that fribidi allows doing that on linux, but I couldn't find any solutions for this problem for Windows.

I would welcome any suggestions, including an alternative to iTextSharp that would do this automatically (if one exists).


回答1:


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;



回答2:


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



回答3:


Just put the string in table cell:

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



回答4:


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.



来源:https://stackoverflow.com/questions/1553748/reversing-strings-in-right-to-left-bidirectional-languages-in-itextsharp

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