温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - How to use any Hindi Font in Itext7?
c# fonts itext7 true-type-fonts

c# - 如何在Itext7中使用任何印地语字体?

发布于 2020-05-10 23:17:24

我正在使用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种字体。

查看更多

提问者
Divyanshu Agarwal
被浏览
75
mkl 2020-02-22 02:22

首先,要检索以前注册的字体,请使用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();