0

I have already tried addFontFile from Resources and it didn't help me. I've also tried to find other pages on this topic to no avail.

I have two font files (.ttf) that I've included as resource files in my VS2017 project. Now I need to load these files (I'm using iTextSharp). I don't see how to do it. It's clearly not as simple as

font = BaseFont.CreateFont("Resources/OpenSans-Regular.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);

because that fires an exception.

Does anyone know how to read a .ttf resource from within Visual Studio? Thanks.

Barry Dysert
  • 665
  • 1
  • 9
  • 21
  • 1
    *"that fires an exception..."* I can't see the exception message and stack trace. And did you even try using `GetManifestResourceStream()` as indicated in your linked post? – Dan Wilson Sep 06 '18 at 14:27
  • 1
    Did you try the second answer in your linked SO question? https://stackoverflow.com/a/23658552/1291628 – ikkentim Sep 06 '18 at 14:30
  • Somewhat. What I need is to access the *file*. That question deals with Streams. – Barry Dysert Sep 06 '18 at 14:33
  • 1
    but if its compiled in as a resource, its not a file any more – BugFinder Sep 06 '18 at 14:34
  • That's a good comment, @BugFinder. But I don't think I can use a stream to create a new font. I could use the stream to create a file, but this is going to run on Azure, and I doubt I have permissions to write to the file system. – Barry Dysert Sep 06 '18 at 14:41

1 Answers1

0

Well I figured it out, so to anyone who wants to do this, you just need two lines:

byte[] fontRegular = Properties.Resources.OpenSans_Regular;
BaseFont font = BaseFont.CreateFont("OpenSans-Regular.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, BaseFont.CACHED, fontRegular, null);

This assumes that the .ttf file is a resource in your project, and it is named OpenSans-Regular.ttf. This lets you use the font in iTextSharp. Enjoy!

Barry Dysert
  • 665
  • 1
  • 9
  • 21