allow arabic text in pdf table using itext7 (xamarin android)

我只是一个虾纸丫 提交于 2021-01-29 07:11:07

问题


I have to put my list data in a table in a pdf file. My data has some Arabic words. When my pdf is generated, the Arabic words don't appear. I searched and found that I need itext7.pdfcalligraph so I installed it in my app. I found this code too https://itextpdf.com/en/blog/technical-notes/displaying-text-different-languages-single-pdf-document and tried to do something similar to allow Arabic words in my table but I couldn't figure it out.

This is a trial code before I apply it to my real list:

var path2 = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
filePath = System.IO.Path.Combine(path2.ToString(), "myfile2.pdf");
stream = new FileStream(filePath, FileMode.Create);
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf2 = new iText.Kernel.Pdf.PdfDocument(writer);
            
Document document = new Document(pdf2, PageSize.A4);
FontSet set = new FontSet();
set.AddFont("ARIAL.TTF");


document.SetFontProvider(new FontProvider(set));
document.SetProperty(Property.FONT, "Arial");
 
string[] sources = new string[] { "يوم","شهر 2020" };
 iText.Layout.Element.Table table = new iText.Layout.Element.Table(2, false);
 foreach (string source in sources)
                {
                    Paragraph paragraph = new Paragraph();
                    Bidi bidi = new Bidi(source, Bidi.DirectionDefaultLeftToRight);
                    if (bidi.BaseLevel != 0)
                    {
                        paragraph.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT);
                    }

                    paragraph.Add(source);
                    table.AddCell(new Cell(1, 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(paragraph));
                }
                document.Add(table);
                document.Close();

I updated my code and added the arial.ttf to my assets folder . i'm getting the following exception:

System.InvalidOperationException: 'FontProvider and FontSet are empty. Cannot resolve font family name (see ElementPropertyContainer#setFontFamily) without initialized FontProvider (see RootElement#setFontProvider).' and I still can't figure it out. any ideas? thanks in advance


回答1:


- C #

I have a similar situation for Turkish characters, and I've followed these steps :

  • Create a folder under projects root folder which is : /wwwroot/Fonts
  • Add OpenSans-Regular.ttf under the Fonts folder

Path for font is => ../wwwroot/Fonts/OpenSans-Regular.ttf

  • Create font like below :
public static PdfFont CreateOpenSansRegularFont()
{
    var path = "{Your absolute path for FONT}";
    return PdfFontFactory.CreateFont(path, PdfEncodings.IDENTITY_H, true);
}

and use it like :

paragraph.Add(source)
         .SetFont(FontFactory.CreateOpenSansRegularFont());  //set font in here

table.AddCell(new Cell(1, 1)
     .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
     .Add(paragraph));

This is how I used font factory for Turkish characters ex: "ü,i,ç,ş,ö"

For Xamarin-Android, you could try

string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
var path = Path.Combine(documentsPath, "Fonts/Arial.ttf");


来源:https://stackoverflow.com/questions/63780908/allow-arabic-text-in-pdf-table-using-itext7-xamarin-android

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