PDFsharp add private/installed font

2019-02-25 07:44发布

问题:

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

回答1:

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



标签: pdfsharp