I have to write a table in an itext7 pdf file. the table must include Arabic words. I've been trying to solve this problem for hours. I found this code so I was trying it:
string[] sources = new string[] { "english.xml", "arabic.xml", "hindi.xml", "tamil.xml" };
PdfWriter writer = new PdfWriter(DEST);
PdfDocument pdfDocument = new PdfDocument(writer);
Document document = new Document(pdfDocument);
FontSet set = new FontSet();
set.AddFont("NotoNaskhArabic-Regular.ttf");
set.AddFont("NotoSansTamil-Regular.ttf");
set.AddFont("FreeSans.ttf");
document.SetFontProvider(new FontProvider(set));
document.SetProperty(Property.FONT, new String[] { "MyFontFamilyName" });
foreach (string source in sources)
{
XmlDocument doc = new XmlDocument();
var stream = new FileStream(source, FileMode.Open);
doc.Load(stream);
XmlNode element = doc.GetElementsByTagName("text").Item(0);
Paragraph paragraph = new Paragraph();
XmlNode textDirectionElement = element.Attributes.GetNamedItem("direction");
Boolean rtl = textDirectionElement != null && textDirectionElement.InnerText.Equals("rtl");
if (rtl)
{
paragraph.SetTextAlignment(TextAlignment.RIGHT);
}
paragraph.Add(element.InnerText);
document.Add(paragraph);
}
document.Close();
I got it from https://itextpdf.com/en/blog/technical-notes/displaying-text-different-languages-single-pdf-document. I started getting exceptions that my FontSet and FontSet provider are empty so I guessed that I don't have the fonts mentioned in the code. I started looking for a way to install them aand I found this answer Installing and using a specific font in a winform so I addeded the following code:
public class user
{
[DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true)]
public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
string lpFileName);
}
and this in my main avtivity:
var result = user.AddFontResource(@"C:\MY_FONT_LOCATION\NotoNaskhArabic-Regular.ttf");
but it didn't work, I got the exception: System.DllNotFoundException: 'gdi32.dll assembly: type: member:(null)'. what should I do? am I on the right path? thanks in advance