我正在使用iText 7.net根据用户语言选择输入以印地语或英语创建pdf,但是我无法找到将我选择的印地语.ttf字体文件转换为itext Pdffonts的任何方法。使用标准的Itext字体,英语效果很好。
这是我的代码:
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();
在将pdf第二行保存在顶部时,代码给出了错误。变量HindiFont1保存印地语字体.ttf文件。
Title9段落中的字符串是英语短语的翻译。
谁能帮我使用印地文字体?我要使用4-5种字体。
首先,要检索以前注册的字体,请使用PdfFontFactory
方法CreateRegisteredFont
代替CreateFont
。因此,更换
PdfFont myHindiFont1 = PdfFontFactory.CreateFont("HindiFont1", PdfEncodings.IDENTITY_H, true);
通过
PdfFont myHindiFont1 = PdfFontFactory.CreateRegisteredFont("HindiFont1", PdfEncodings.IDENTITY_H, true);
然后,如果您想将文本添加到要以特定字体绘制的段落中,请先设置字体,然后添加文本。因此,代替
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph("ew[;kad fo'kslrk;sa%");
Title9.SetFont(myHindiFont1);
做
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph().SetFont(myHindiFont1).Add("ew[;kad fo'kslrk;sa%");
或者,您可以将该字体设置为文档默认字体:
Document document = new Document(pdf, size);
document.SetFont(myHindiFont1);
iText.Layout.Element.Paragraph Title9 = new iText.Layout.Element.Paragraph("ew[;kad fo'kslrk;sa%");
最后,将您的新段落添加到某个实体,例如
document.Add(Title9);
结果:
这里是我用来成功渲染上述屏幕截图的最终代码:
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();
我现在要尝试一下。手指交叉。
好吧,我不知道您是如何工作的,但是我会抛出此错误。
NullReferenceException:对象引用未设置为对象iText.Layout.Renderer.TextRenderer.ConvertToGlyphLine的实例(System.String文本)(位于<0b6ebc25fe5349debfa3cd4517a89bd7>:0)iText.Layout.Renderer.TextRenderer.ConvertWaitingStringToGlyph66(5) >:0)iText.Layout.Renderer.TextRenderer.ApplyOtf()(位于<0b6ebc25fe5349debfa3cd4517a89bd7>:0)
iText.Layout.Renderer.LineRenderer.Layout(iText.Layout.Layout.LayoutContext layoutContext)(在<0b6ebc25fe5349debfa3cd4517a89bd7>:0处)iText.Layout.Renderer.ParagraphRenderer.Layout(iText.Layout.Layout.LayoutContext layoutContext)(在<0b6bcbfa43d45fa5d4b17bc3fe5349 >:0)iText.Layout.Renderer.RootRenderer.AddChild(iText.Layout.Renderer.IRenderer渲染器)(在<0b6ebc25fe5349debfa3cd4517a89bd7>:0处)iText.Layout.RootElement
1[T].Add[T2] (iText.Layout.Element.BlockElement
1 [T]元素)(在<0b6ebc25fe5349debfa3cd4517>iText.Layout.Document.Add [T](iText.Layout.Element.BlockElement`1 [T]元素)(在<0b6ebc25fe5349debfa3cd4517a89bd7>:0处)Panel.HindiPdf()(在Assets / Scripts / Panel.cs:7123处) Panel + <ShowSaveDialogCoroutine> d__112.MoveNext()(在Assets / Scripts / Panel.cs:6791)UnityEngine.SetupCoroutine.InvokeMoveNext(System.Collections.IEnumerator枚举器,System.IntPtr returnValueAddress)(在C:/ buildslave / unity / build /运行时/导出/Coroutines.cs:17)