FontSelector.Process 的 iText 7 等价物是什么?
What is the iText 7 equivalent of FontSelector.Process?
我正在开始更新针对 iText5 编写的代码以与 iText7 一起使用。
iText7 中是否有与 FontSelector class 类似的机制,您可以在其中加载字体,"Process" 操作会自动确定要使用的字体(并且 return可以添加到文档中的格式化 pdf "chunk")?这是代码片段(这是 C++,但我的 "native" 语言是 C#,所以请随意用 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 确实有类似的机制。甚至还有一种方法可以使其隐式工作,而无需处理块并将它们手动添加到文档中。您要找的 class 叫做 FontProvider
。首先,您需要创建一个实例并将您的字体添加到其中:
FontProvider provider = new FontProvider();
provider.AddFont(fontsFolder + "NotoSans-Regular.ttf");
provider.AddFont(fontsFolder + "FreeSans.ttf");
provider.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H);
然后,您需要 layout
的 Document
实例,它可能是这样创建的,或者以任何其他方式创建的:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);
然后,您只需为文档设置字体提供程序,最重要的是,设置首选字体名称以触发字体select或机制。如果您没有 select 首选字体名称并且没有明确设置 PdfFont
,将使用默认字体 Helvetica
。
执行上述操作的代码可能如下所示:
doc.SetFontProvider(provider);
doc.SetFont("NotoSans");
Paragraph paragraph = new Paragraph("Hello world! \u05E2\u05B4\u05D1\u05B0\u05E8\u05B4\u05D9\u05EA\u202C");
doc.Add(paragraph);
最后,别忘了关闭文档:
doc.Close();
我正在开始更新针对 iText5 编写的代码以与 iText7 一起使用。
iText7 中是否有与 FontSelector class 类似的机制,您可以在其中加载字体,"Process" 操作会自动确定要使用的字体(并且 return可以添加到文档中的格式化 pdf "chunk")?这是代码片段(这是 C++,但我的 "native" 语言是 C#,所以请随意用 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 确实有类似的机制。甚至还有一种方法可以使其隐式工作,而无需处理块并将它们手动添加到文档中。您要找的 class 叫做 FontProvider
。首先,您需要创建一个实例并将您的字体添加到其中:
FontProvider provider = new FontProvider();
provider.AddFont(fontsFolder + "NotoSans-Regular.ttf");
provider.AddFont(fontsFolder + "FreeSans.ttf");
provider.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H);
然后,您需要 layout
的 Document
实例,它可能是这样创建的,或者以任何其他方式创建的:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);
然后,您只需为文档设置字体提供程序,最重要的是,设置首选字体名称以触发字体select或机制。如果您没有 select 首选字体名称并且没有明确设置 PdfFont
,将使用默认字体 Helvetica
。
执行上述操作的代码可能如下所示:
doc.SetFontProvider(provider);
doc.SetFont("NotoSans");
Paragraph paragraph = new Paragraph("Hello world! \u05E2\u05B4\u05D1\u05B0\u05E8\u05B4\u05D9\u05EA\u202C");
doc.Add(paragraph);
最后,别忘了关闭文档:
doc.Close();