【问题标题】:Create font from URL从 URL 创建字体
【发布时间】:2019-02-09 20:06:41
【问题描述】:

是否有机会从 URI 创建字体?比如:

// c# code
string fontUri = "https://www.manyfonts.com/VAGRoundedStd-Thin.ttf";
BaseFont myfont = BaseFont.CreateFont(fontUri, BaseFont.CP1252, BaseFont.EMBEDDED);

// or

Font font = FontFactory.GetFont(fontUri, BaseFont.CP1252,false, 9);

我也试过二进制

public static Font GetFont()
{

  string fontUri = Config.FONT_URI;
  Console.WriteLine(fontUri);
  byte[] fontBinary = new WebClient().DownloadData(fontUri);
  BaseFont bf = BaseFont.CreateFont(
    "VAGRoundedStd-Thin.ttf",
    BaseFont.WINANSI,
    BaseFont.EMBEDDED,
    false,
    fontBinary,
    null
  );

  return new Font(bf, 12, Font.NORMAL, Colors.PINK);
}

现在我明白了:

Unhandled Exception: System.NotSupportedException: No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
   at System.Text.Encoding.GetEncoding(Int32 codepage)
   at iTextSharp.text.pdf.TrueTypeFont.ReadStandardString(Int32 length)
   at iTextSharp.text.pdf.TrueTypeFont.Process(Byte[] ttfAfm, Boolean preload)
   at iTextSharp.text.pdf.TrueTypeFont..ctor(String ttFile, String enc, Boolean emb, Byte[] ttfAfm, Boolean justNames, Boolean forceRead)

我的代码在 lambda 函数中,无法访问文件系统。也许将 ttf 加载到内存中,然后以某种方式加载到 iTextSharp 中?欢迎任何解决方法。

【问题讨论】:

  • 为什么不使用WebClientdownload the font,然后将其作为byte[] 传递给iTextSharp? See also.
  • 嗨@UweKeim 谢谢你的回答,是的,它接缝可以这样做......但是我在使用BaseFont.createFont 时遇到错误我用新代码编辑了这个问题!
  • “但是我在使用 BaseFont.createFont 时遇到错误” - 考虑在方法名称中使用大写的“c”:BaseFont.CreateFont
  • 请注意您使用的是旧版本的 iText。对该版本的支持已停止。你应该升级到iText 7
  • 你在写@mkl。现在我收到下一个错误:No data is available for encoding 1252

标签: c# fonts aws-lambda itext


【解决方案1】:

我会问自己。感谢@UweKeim @mkl @bruno 的帮助。

如果您在使用 not supported exception 时遇到问题(可能是因为您使用的是 mac 或 linux),请将此引用添加到您的 .csproj

<PackageReference Include="System.Text.Encoding.CodePages" />

这里是一个从添加到单元格文本的 URL 中创建字体的 sn-p 示例。

using System;
using System.Net;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace MyNameSpace
{
  class Foo
  {
    public static PdfPCell CreateCellWithFontFromUrl()
    {

      // Necesary ONLY if you're getting the error `not supported encoding`
      System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

      string fontUri ="http://www.fonts.com/yourfontname.ttf";
      byte[] fontBinary = new WebClient().DownloadData(fontUri);

      BaseFont bf = BaseFont.CreateFont(
        "VAGRoundedStd-Thin.ttf", // Is important you add ".ttf" at the end of the font name
        BaseFont.WINANSI,
        BaseFont.EMBEDDED,
        false, // NO CACHE
        fontBinary,
        null
      );

      return bf;
    };

    Font MY_FONT = new Font(bf, 20, Font.BOLD, new Color(29, 29, 29));
    PdfPCell cell = new PdfPCell(new Paragraph("text of the paragraph", MY_FONT));

    return cell;
  }
}

【讨论】:

    猜你喜欢
    • 2018-07-19
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2018-12-10
    • 1970-01-01
    • 2014-07-04
    相关资源
    最近更新 更多