允许使用itext7在pdf表中使用阿拉伯语文本(xamarin android) [英] allow arabic text in pdf table using itext7 (xamarin android)
问题描述
我必须将列表数据放在pdf文件的表格中.我的数据有一些阿拉伯语单词.生成我的pdf时,没有出现阿拉伯语单词.我搜索后发现需要itext7.pdfcalligraph,因此将其安装在我的应用程序中.我也找到了此代码 https://itextpdf.com/zh-CN/blog/technical-notes/displaying-text-different-languages-single-pdf-document ,并尝试做类似的操作以允许阿拉伯语出现在我的表格中,但我无法弄清楚
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();
我更新了代码,并将arial.ttf添加到资产文件夹中.我收到以下异常:
I updated my code and added the arial.ttf to my assets folder . i'm getting the following exception:
System.InvalidOperationException:'FontProvider和FontSet为空.如果未初始化FontProvider(请参阅RootElement#setFontProvider),则无法解析字体系列名称(请参阅ElementPropertyContainer#setFontFamily). 而且我仍然无法弄清楚.有任何想法吗? 预先感谢
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
推荐答案
-C#
对于土耳其语字符,我也有类似情况,并且我遵循了这些规则 步骤:
I have a similar situation for Turkish characters, and I've followed these steps :
- 在项目根文件夹下创建一个文件夹:/wwwroot/Fonts
- 在Fonts文件夹下添加
OpenSans-Regular.ttf
字体路径为=> ../wwwroot/Fonts/OpenSans-Regular.ttf
Path for font is => ../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,ç,ş,ö"
This is how I used font factory for Turkish characters ex: "ü,i,ç,ş,ö"
对于Xamarin-Android,您可以尝试
For Xamarin-Android, you could try
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var path = Path.Combine(documentsPath, "Fonts/Arial.ttf");
这篇关于允许使用itext7在pdf表中使用阿拉伯语文本(xamarin android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!