I am using iText 7.net to create a pdf in Hindi or english based on user language selection input but I can't figure out any way to convert my selected Hindi .ttf font files to itext Pdffonts. It works fine in English with standard Itext Fonts.
This is my code:
PdfFontFactory.Register(HindiFont1.ToString(), "HindiFont1");
//Error at this line: Font Not Recognized
PdfFont myHindiFont1 = PdfFontFactory.CreateFont("HindiFont1", PdfEncodings.IDENTITY_H, true);
//Create Writer
PdfWriter writer = new PdfWriter(path);
//Create Pdf Document Object
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, size);
PdfPage page1 = pdf.AddNewPage();
PdfCanvas canvas3 = new PdfCanvas(page3);
Rectangle pageSize3 = page3.GetPageSize();
//String in Title9 Paragraph is a translation of English Phrase
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph("ew[;kad fo'kslrk;sa%");
Title9.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
// Title9.SetFont(myHindiFont1);
document.Close();
The code gives error while saving pdf second line at the top. variable HindiFont1 holds the Hindi Font .ttf file.
String in Title9 Paragraph is a translation of English Phrase.
Can anyone help in using my hindi fonts? I have 4-5 fonts I want to use.
First of all, to retrieve a previously registered font use the PdfFontFactory
method CreateRegisteredFont
instead of CreateFont
. Thus, replace
PdfFont myHindiFont1 = PdfFontFactory.CreateFont("HindiFont1", PdfEncodings.IDENTITY_H, true);
by
PdfFont myHindiFont1 = PdfFontFactory.CreateRegisteredFont("HindiFont1", PdfEncodings.IDENTITY_H, true);
Then, if you want to add text to a paragraph to be drawn in a specific font, first set the font, then add the text. Thus, instead of
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph("ew[;kad fo'kslrk;sa%");
Title9.SetFont(myHindiFont1);
do
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph().SetFont(myHindiFont1).Add("ew[;kad fo'kslrk;sa%");
Alternatively you can set that font as document default font:
Document document = new Document(pdf, size);
document.SetFont(myHindiFont1);
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph("ew[;kad fo'kslrk;sa%");
And finally, add your new paragraph to some entity, e.g.
document.Add(Title9);
The result:
Here the final code I used to successfully render the above screen shot:
String HindiFont1 = @"LEOPALMHINDI15K710.TTF";
PageSize size = PageSize.A4;
PdfFontFactory.Register(HindiFont1, "HindiFont1");
//Error at this line: Font Not Recognized
PdfFont myHindiFont1 = PdfFontFactory.CreateRegisteredFont("HindiFont1", PdfEncodings.IDENTITY_H, true);
//Create Writer
PdfWriter writer = new PdfWriter(@"UseLeopalmhindi15K710LikeDivyanshuAgarwalImproved.pdf");
//Create Pdf Document Object
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, size);
//document.SetFont(myHindiFont1);
//String in Title9 Paragraph is a translation of English Phrase
//iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph("ew[;kad fo'kslrk;sa%");
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph().SetFont(myHindiFont1).Add("ew[;kad fo'kslrk;sa%");
Title9.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
document.Add(Title9);
document.Close();