如何使用 iText 7 设置卢布符号?

How to set ruble symbol with iText 7?

我使用 iText 7.1.4 并且经常在其中使用俄语字体。我想在我的 PDF 文件中设置卢布符号,但我不能。

这是代码片段:

tableVariations.AddCell(priceSettings.HasLines ?
  new TextCell(
    $"{variation.Price}₽",
    PdfFontFactory.CreateFont(priceSettings.ContentFont, "Cp1251", true),
    priceSettings.ContentFontSize,
    priceSettings.ContentForegroundColor.ToDeviceRgb(),
    priceSettings.LinesColor.ToDeviceRgb(),
    priceSettings.LinesThickness,
    TextAlignment.RIGHT) :
  new TextCell(
    $"{variation.Price}₽",
    PdfFontFactory.CreateFont(priceSettings.ContentFont, "Cp1251", true),
    priceSettings.ContentFontSize,
    priceSettings.ContentForegroundColor.ToDeviceRgb(),
    TextAlignment.RIGHT));

其中 tableVariationsTablepriceSettings.ContentFont 是俄语字体。

我只能在结果 PDF 文件中看到价格,但没有出现 符号。

我这样试过:

new TextCell(
    $"{variation.Price}₽",
    PdfFontFactory.CreateFont(priceSettings.ContentFont, PdfEncodings.UNICODE_BIG, true));

new TextCell(
    $"{variation.Price}\u20BD",
    PdfFontFactory.CreateFont(priceSettings.ContentFont, PdfEncodings.UNICODE_BIG, true));

new TextCell(
    $"{variation.Price}\u20BD",
    PdfFontFactory.CreateFont(priceSettings.ContentFont, PdfEncodings.UTF8, true));

new TextCell(
    $"{variation.Price}₽",
    PdfFontFactory.CreateFont(priceSettings.ContentFont, PdfEncodings.UTF8, true));

我没有得到任何结果...

让我们按照官方的 iText7 示例(它是用 Java 编写的,但由于 iText 是从 Java 自动移植到 C# 的,因此应该没有区别): https://github.com/itext/i7js-examples/blob/develop/src/test/java/com/itextpdf/samples/sandbox/fonts/tutorial/F05_Russian_correct_encoding.java

让我们将卢布符号添加到文本中,看看它不会用 FreeSans 很好地呈现。原因是字体内部没有卢布符号,FontForge证明了这个假设:

现在让我们使用windows中提供的Arial字体。 FontForge 显示该符号是在该字体内处理的:

现在让我们将编码更改为IDENTITY_H:

PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);

并看到一切都已正确处理:

所以我建议你检查一下你的字体。里面好像没有卢布符号。