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

允许使用itext7在pdf表中使用阿拉伯文字xamarin android

如何解决允许使用itext7在pdf表中使用阿拉伯文字xamarin android

我必须将列表数据放在pdf文件的表格中。我的数据有一些阿拉伯语单词。生成我的pdf时,没有出现阿拉伯语单词。我搜索后发现需要itext7.pdfcalligraph,因此将其安装在我的应用程序中。我也找到了这段代码https://itextpdf.com/en/blog/technical-notes/displaying-text-different-languages-single-pdf-document,并尝试做类似的操作以允许在我的表中使用阿拉伯语单词,但我无法弄清楚。

在我将其应用于真实列表之前,这是一个试用代码

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

我更新了代码,并将arial.ttf添加到资产文件夹中。我收到以下异常:

system.invalidOperationException:'FontProvider和FontSet为空。如果未初始化FontProvider(请参阅RootElement#setFontProvider),则无法解析字体系列名称(请参阅ElementPropertyContainer#setFontFamily)。 而且我仍然无法弄清楚。有任何想法吗? 预先感谢

解决方法

-C#

对于土耳其语字符,我也有类似情况,并且我遵循了这些规则 步骤:

  • 在项目根文件夹下创建一个文件夹:/ wwwroot / Fonts
  • 在Fonts文件夹下添加OpenSans-Regular.ttf

字体路径为=> ../wwwroot/Fonts/OpenSans-Regular.ttf

  • 创建如下字体:
public static PdfFont CreateOpenSansRegularFont()
{
    var path = "{Your absolute path for FONT}";
    return PdfFontFactory.CreateFont(path,PdfEncodings.IDENTITY_H,true);
}

并像这样使用它:

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

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

这就是我将字体工厂用于土耳其字符的方式,例如:“ü,i,ç,ş,ö”

对于Xamarin-Android,您可以尝试

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

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