什么是FontSelector.Process的iText 7等效项? [英] What is the iText 7 equivalent of FontSelector.Process?

查看:174
本文介绍了什么是FontSelector.Process的iText 7等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始更新针对iText5编写的代码以与iText7一起使用的过程.

I'm beginning the process of updating code written against iText5 to work with iText7.

iText7中是否有与FontSelector类类似的机制,您可以在其中加载字体,并且"Process"操作将自动确定要使用的字体(并返回可以添加到文档中的格式化的pdf块" )?这是代码段(这是C ++,但是我的本机"语言是C#,因此请随时使用C#进行回答.)

Is there a similar mechanism in iText7 to the FontSelector class, where you would load up fonts and the "Process" action would automatically determine which font to use (and return a formatted pdf "chunk" that could be added to the document)? Here's the code snippet (this is C++, but my "native" language is C#, so feel free to answer with C#).

try {
    doc = gcnew Document();
    pdfWriter = PdfWriter::GetInstance(doc, pdfStream);

    FontSelector^ selector = gcnew FontSelector();
    selector->AddFont(gcnew Font(BaseFont::CreateFont("Fonts\\cour.ttf"), BaseFont::IDENTITY_H, BaseFont::NOT_EMBEDDED), 10.0f));
    selector->AddFont(gcnew Font(BaseFont::CreateFont("Fonts\\arialuni.ttf"), BaseFont::IDENTITY_H, BaseFont::NOT_EMBEDDED), 10.0f));

    doc->Open();
    while (textReader->EndOfStream == false)
    {
        String^ line = textReader->ReadLine();
        doc->Add(selector->Process(line + "\n"));
}

推荐答案

iText7中确实可以使用类似的机制.甚至有一种方法可以使它隐式工作,而无需处理块并将其手动添加到文档中.您要查找的类称为FontProvider.首先,您需要创建一个实例并向其中添加字体:

A similar mechanism is indeed available in iText7. And there is even a way to make it work implicitly without processing chunks and adding them manually to the document. The class you are looking for is called FontProvider. To start with, you need to create an instance and add your fonts to it:

FontProvider provider = new FontProvider();
provider.AddFont(fontsFolder + "NotoSans-Regular.ttf");
provider.AddFont(fontsFolder + "FreeSans.ttf");
provider.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H);

然后,您需要layoutDocument实例,该实例可能是这样创建的或以其他任何方式创建的:

Then, you need layout's Document instance which might have been created like this, or in any other way:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);

然后,您所需要做的就是为文档设置字体提供程序,最重要的是,设置首选字体名称以触发字体选择器机制.如果您未选择首选字体名称并且未明确设置PdfFont,则将使用默认字体Helvetica.

Then, all you need is set the font provider to the document and, most importantly, set the preferred font name to trigger the font selector mechanism. If you don't select the preferred font name and don't set PdfFont explicitly, the default font, Helvetica, will be used.

执行上述操作的代码如下:

The code to do the above might look like following:

doc.SetFontProvider(provider);
doc.SetFont("NotoSans");
Paragraph paragraph = new Paragraph("Hello world! \u05E2\u05B4\u05D1\u05B0\u05E8\u05B4\u05D9\u05EA\u202C");
doc.Add(paragraph);

最后,不要忘记关闭文档:

Finally, don't forget to close the document:

doc.Close();

这篇关于什么是FontSelector.Process的iText 7等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆