Pdf System.IO.IOException:ttf文件的路径未作为文件或资源找到;(xamarin.android)

Pdf System.IO.IOException:ttf文件的路径未作为文件或资源找到;(xamarin.android),pdf,xamarin.android,itext7,Pdf,Xamarin.android,Itext7,我正在尝试为我的itext7使用自定义字体,以允许我的pdf编写阿拉伯语文本。我所做的是: var path2 = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; filePath = System.IO.Path.Combine(path2.ToString(), "myfile3.pdf"); stream = new FileStream(filePath, FileMode.C

我正在尝试为我的itext7使用自定义字体,以允许我的pdf编写阿拉伯语文本。我所做的是:

var path2 = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
filePath = System.IO.Path.Combine(path2.ToString(), "myfile3.pdf");
stream = new FileStream(filePath, FileMode.Create);
iText.Layout.Element.Table table = new iText.Layout.Element.Table(3, false);

table.SetWidth(400).SetFixedLayout();
string[] sources = new string[] { "يوم","شهر 2020" };
PdfWriter writer2 = new PdfWriter(stream);
PdfDocument pdfDocument = new PdfDocument(writer2);
Document document2 = new Document(pdfDocument);
PdfFont arab= PdfFontFactory.CreateFont("NotoNaskhArabic-Regular.ttf");
document2.SetFont(arab);
 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));
                    }
                    document2.Add(table);
                    document2.Close();

我尝试了不同的字体路径。我把它放在我的资源文件夹中,在我的资产中,当我尝试使用ARIAL字体时,我试图从
C:\Windows\Fonts\ARIAL.TTF
访问它,但所有这些都不起作用,我不明白,我的路径应该是什么,这样我就不会出现此异常:System.IO.IOException:ttf文件的路径未作为文件或资源找到。

一旦将文件放入资源中,您可以使用
AssetManager
从字体中读取字节。然后按字节创建字体


您可以引用并将其转换为C代码。

请使用
File.Exists
检查文件是否真的存在,然后再将该路径发送到iText。它现在可以工作吗?不,先生,它仍然不能工作,我尝试了File.Exists在控制台应用程序中,它返回true,因为我的文件夹在我的pc上,但对于android应用程序,它返回false对于android应用程序,你把文件放进资产文件夹了吗?并使用AssetManager读取它;AssetManager资产=此.assets;字节[]字节=新字节[0];使用(StreamReader sr=new StreamReader(assets.Open(“notonasharabic Regular.ttf”){content=sr.ReadToEnd();bytes=Encoding.ASCII.GetBytes(content);}PdfFont russic=PdfFontFactory.CreateFont(bytes,false)现在他告诉我
itext.io.ioexception:“字体类型无法识别。”
虽然我从书法文档下载了我的字体如何检查?您可以在获取字节的行上设置断点。