1

Is there a chance to create a font from URI? Something like:

// 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);

I also tried in binary

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);
}

Now I get:

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)

My code is in a lambda function and cannot access filesystem. Maybe loading the ttf in memory and then somehow in iTextSharp? any workaround is welcome.

R01010010
  • 5,670
  • 11
  • 47
  • 77
  • 2
    Why not use `WebClient` to [download the font](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloaddata) before passing it as a `byte[]`to iTextSharp? [See also](https://stackoverflow.com/a/20540464/107625). – Uwe Keim Sep 04 '18 at 18:59
  • Hi @UweKeim thank you for the answer, yes it seams it's possible to do so... however I'm getting an error when using `BaseFont.createFont` I edited the question with the new code! – R01010010 Sep 04 '18 at 19:29
  • 2
    *"however I'm getting an error when using BaseFont.createFont"* - consider using a capital 'c' in the method name: `BaseFont.CreateFont`. – mkl Sep 05 '18 at 04:41
  • Please be aware that you are using an old version of iText. Support for that version has been discontinued. You should upgrade to [iText 7](https://www.nuget.org/packages/itext7/). – Bruno Lowagie Sep 05 '18 at 10:09
  • you're write @mkl. Now i'm getting the next error: `No data is available for encoding 1252` – R01010010 Sep 09 '18 at 12:37
  • @R01010010 Which iTextSharp version exactly do you use? The stack trace does not seem to match exactly (but probably code was inlined). That been said, are you sure AWS Lambda does support the "windows-1252" encoding? – mkl Sep 10 '18 at 14:38
  • I'm actually developing on a mac. Didn't try aws lambda env yet. I'm using this one: https://github.com/schourode/iTextSharp-LGPL I suppose I need to add the encoding to the System.Text.Encoding.GetEncoding(Int32 codepage) ? – R01010010 Sep 11 '18 at 12:10

1 Answers1

0

I'm gonna ask myself. Thanks @UweKeim @mkl @bruno for your help.

If you're having problems with the not supported exception (probably cause you're in mac or linux) add this reference to your .csproj.

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

And here is a snippet example of a working creation of a font from a URL added to a cell text.

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;
  }
}
R01010010
  • 5,670
  • 11
  • 47
  • 77