0

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

rana hd
  • 355
  • 3
  • 18
  • You don't need that `DllImport` for iText, just check your `set.AddFont` to return `true`. If it returns `false` then you probably provide incorrect path. – Alexey Subach Sep 09 '20 at 21:21
  • yes, it returns false, but I can't figure why, I put my ttf file in assets, in resources and tried different paths but it still doesn't work. would you please give me an idea on where should my font be and how to access it – rana hd Sep 10 '20 at 06:47
  • Use `File.Exists` C# function to check if the file can be found on the file system given its path. If it is found but `set.AddFont` returns false then problem is in iText. If the file cannot be found then the problem is in the file path you provide – Alexey Subach Sep 10 '20 at 12:23
  • sir I really find it strange that none of my pc's paths are working. I tried this code:```FontSet set = new FontSet(); int totalfonts = PdfFontFactory.RegisterDirectory(@"C:\Users\User\Desktop\fonts"); if (totalfonts > 0) { Toast.MakeText(this, totalfonts.ToString(), ToastLength.Long); foreach (string font in PdfFontFactory.GetRegisteredFonts()) { set.AddFont(font); Toast.MakeText(this, font, ToastLength.Long); }document2.SetFontProvider(new FontProvider(set));``` but I got my totalfonts=0. is this possible that no path is working? – rana hd Sep 10 '20 at 12:36
  • Plase run `File.Exists` function against your path to check whether .NET can find it – Alexey Subach Sep 10 '20 at 19:21
  • I used File.Exists using console app against my path and it returned true. – rana hd Sep 13 '20 at 07:25

0 Answers0