微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

System.IO.IOException:找不到文件或资源作为ttf文件的路径'xamarin.android

如何解决System.IO.IOException:找不到文件或资源作为ttf文件的路径'xamarin.android

我正在尝试为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从字体中读取字节。然后按字节创建字体。

您可以参考this并将其转换为C#代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。