OiO.lk Blog pdf Set custom font in iText7 C# .NET MAUI Project for PDF
pdf

Set custom font in iText7 C# .NET MAUI Project for PDF


I am trying to add my custom font to this example in Maui but it always fail, I have checked the font family name online and it is correct, has anyone worked with this example here?

// Get the application directory
string appDirectory = FileSystem.AppDataDirectory;

// Construct the full path to your font file
string fontFilePath = Path.Combine(appDirectory, "Resources", "Raw", "pdms-saleem-quranfont.ttf");

PdfWriter writer = new PdfWriter(filePath);
PdfDocument pdfDocument = new PdfDocument(writer);
Document document = new Document(pdfDocument);
FontSet set = new FontSet();
set.AddFont(fontFilePath);
//set.AddFont("NotoSansTamil-Regular.ttf");
//set.AddFont("FreeSans.ttf");
document.SetFontProvider(new FontProvider(set));
document.SetProperty(Property.FONT, new String[] { "_PDMS_Saleem_QuranFont" });
Paragraph paragraph = new Paragraph();
paragraph.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT);
paragraph.Add(TheOpeningText);
document.Add(paragraph);

The error:

"FontProvider and FontSet are empty. Cannot resolve font family name (see ElementPropertyContainer#setFontFamily) without initialized FontProvider (see RootElement#setFontProvider)."

I tried adding new fonts to the example found in iText’s documentation but that example itself is not for Maui so I am trying to port that example to work in a Maui project. The expectation is to have a working project that can generate PDF with Arabic text using custom font.

Link to MVP: https://github.com/takasurazeem/MauiGeneratePdfSample/blob/Arabic-Font/MauiPdfJsViewerSample/MainPage.xaml.cs



You need to sign in to view this answers

Exit mobile version