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();
I am gonna give it a try right now. Finger's Crossed.
okay I dont know how you got it to work but throws this error on my end..
NullReferenceException: Object reference not set to an instance of an object iText.Layout.Renderer.TextRenderer.ConvertToGlyphLine (System.String text) (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0) iText.Layout.Renderer.TextRenderer.ConvertWaitingStringToGlyphLine () (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0) iText.Layout.Renderer.TextRenderer.ApplyOtf () (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0)
iText.Layout.Renderer.LineRenderer.Layout (iText.Layout.Layout.LayoutContext layoutContext) (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0) iText.Layout.Renderer.ParagraphRenderer.Layout (iText.Layout.Layout.LayoutContext layoutContext) (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0) iText.Layout.Renderer.RootRenderer.AddChild (iText.Layout.Renderer.IRenderer renderer) (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0) iText.Layout.RootElement
1[T].Add[T2] (iText.Layout.Element.BlockElement
1[T] element) (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0)iText.Layout.Document.Add[T] (iText.Layout.Element.BlockElement`1[T] element) (at <0b6ebc25fe5349debfa3cd4517a89bd7>:0) Panel.HindiPdf () (at Assets/Scripts/Panel.cs:7123) Panel+<ShowSaveDialogCoroutine>d__112.MoveNext () (at Assets/Scripts/Panel.cs:6791) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)