3

I want to apply Trade gothic font to my pdf text using PDFsharp, I have installed the font and use below line of code to apply

XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
// var options = new XPdfFontOptions(PdfFontEmbedding.Always);
XFont font = new XFont("TRADE GOTHIC BOLD CONDENSED NO. 20", 20, XFontStyle.Bold, options);

But it does not work!!. Also I wanted to know in production I'm using Windows server 2008, is there a way I can dynamically add this font in production server even it is not there?

As suggested I followed the pdfsharp forum , this is my sample code

XPrivateFontCollection privateFontCollection = XPrivateFontCollection.Global;
 // Uri fontUri = new Uri(MappedApplicationPath + "Fonts\\trade-gothic-no-20-condensed-bold-1361518746.ttf");
 Uri fontUri = new Uri("C:\\inetpub\\wwwroot\\wss\\VirtualDirectories\\80\\Fonts\\trade-gothic-no-20-condensed-bold-1361518746.ttf");

LoadPrivateFont(privateFontCollection, fontUri, "./#TradeGothicNo.20-Condensed"); 

I tried all possible combination of path and file name , the name as mentioned in .ttf file but still getting exception . I have a sharepoint Visual webpart, and on page load event of that webpart m writing this code..

This is load method

protected void LoadPrivateFont(PdfSharp.Drawing.XPrivateFontCollection privateFontCollection, Uri fontUri, string sFontFamilyname)
        {

            try
            {
                privateFontCollection.Add(fontUri, sFontFamilyname);
            }
            catch
            {
            }
        }

I have followed this post http://forum.pdfsharp.net/viewtopic.php?f=2&t=1880

Thanks

user3178955
  • 33
  • 1
  • 1
  • 8
  • You get an exception, but do not reproduce any exception details here. Is it an Access Denied exception? Or something else? Catch the exception and show ex.ToString() in a message box to get detailed information with a call stack. – I liked the old Stack Overflow Feb 05 '14 at 15:26
  • The Exception is font doesnot exist – user3178955 Feb 08 '14 at 12:24
  • The Uri syntax for fonts is tricky. First make sure the font works in a stand-alone application (based on the Private Fonts sample). Then in step 2 try to get is working in ASP.NET (now that you know the Uri syntax is correct, problems may be caused by insufficient rights, so make sure the Application Pool used for your site can read the font files). – I liked the old Stack Overflow Feb 11 '14 at 13:48

1 Answers1

2

When using fonts with PDFsharp, make sure the font is a TrueType font (not a PostScript font).

Also make sure you write the font name correctly - as shown by the Font applet of Windows or as shown by Word.

You can use a private font collection to use fonts that are not installed on the computer. This should solve your "problem" with Windows Server 2008. Use the WPF build of PDFsharp.

The PDFsharp source package includes a full working sample that uses private fonts.
A code snippet can be seen here:
http://pdfsharp.net/wiki/PrivateFonts-sample.ashx

  • 1
    This example is showing how to add the font, where do i need to call this in my aaplication. also this.fontFamilies what "this" in that case. i have one pdf that i need to send via email and also writting some dynamic text in pdf, i have added reference to PdfSharp dll in my project what else do i need? please suggest!! – user3178955 Feb 04 '14 at 18:23
  • The post correctly says: "The PDFsharp source package includes a full working sample that uses private fonts." Code snippet on the site, full working sample in the source code - please check it out. – I liked the old Stack Overflow Feb 05 '14 at 08:58
  • 2
    The full download doesn't contain a GDI+ implementation either. The documentation is pretty useless as it is. – Knelis Nov 13 '15 at 10:01
  • @Knelis Support for Private Fonts has been changed a lot in the last few weeks for the next version of PDFsharp 1.50 (beta 3). It is work in progress. Once the code is fully functional, the samples and the site will be updated and the source code will be made available. I'm not sure whether private fonts can be used with the GDI+ build of PDFsharp 1.3x. – I liked the old Stack Overflow Nov 13 '15 at 11:22
  • 2
    Examples were never updated. Documentation is pretty much worthless and contains circular references that say "Note: XPrivateFontCollection is no longer supported and no longer recommended. This sample is outdated. Use the IFontResolver interface."[1] with a link to a page that says "Note: When using the GDI build, you can use the XPrivateFontCollection class."[2] with a link back to the first page. [1] http://pdfsharp.net/wiki/PrivateFonts-sample.ashx [2] http://pdfsharp.net/wiki/FontResolver-sample.ashx – Isaac Raway Oct 18 '16 at 13:45
  • @IsaacRaway The "samples" are updated and come in a ZIP file that accompanies the PDFsharp source code. The "documentation" on the website contains only code snippets to illustrate general usage. Some information may be outdated. There were plans to remove XPrivateFontCollection completely, but since it is the only method that works with the GDI build it is still there in beta 3b. Documentation will be updated when 1.50 will be out of beta state. The IFontResolver method did not yet exist when the original question was asked here. My answer applies to version 1.32. – I liked the old Stack Overflow Oct 21 '16 at 14:03