1

I'm trying to use custom fonts for my itext7 to allow my pdf to write Arabic texts. what I did is the following:

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

I tried different paths for my font. I put it in my resources folder, in my assets, tried to reach it from C:\Windows\Fonts\ARIAL.TTF when I tried using arial font, but all of those didn't work, I don't get it, what should my path be so I won't get this exception: System.IO.IOException: path to ttf file not found as file or resource.'

Nimantha
  • 6,405
  • 6
  • 28
  • 69
rana hd
  • 355
  • 3
  • 18

1 Answers1

0

Once you have your file in assets, you could use AssetManager to read the bytes from the font.Then create the font by the bytes.

You could refer to this and convert it to C# code.

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • I tried this:```string content; AssetManager assets = this.Assets; byte[] bytes =new byte[0]; using (StreamReader sr = new StreamReader(assets.Open("NotoNaskhArabic-Regular.ttf"))) { content = sr.ReadToEnd(); bytes = Encoding.ASCII.GetBytes(content); } PdfFont russian = PdfFontFactory.CreateFont(bytes,false);``` now he's telling me ```itext.io.ioexception: 'type of font is not recognized.' ``` though I downloaded my font from the calligraph documentation – rana hd Sep 10 '20 at 07:23
  • how do I check? – rana hd Sep 10 '20 at 11:58
  • You could make a breakpoint on the line which you get the bytes. – Leo Zhu Sep 11 '20 at 01:23