PDFsharp add private/installed font
Asked Answered
D

1

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

Donavon answered 3/2, 2014 at 18:41 Comment(3)
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.Southerland
The Exception is font doesnot existDonavon
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).Southerland
S
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

Southerland answered 4/2, 2014 at 6:32 Comment(6)
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!!Donavon
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.Southerland
The full download doesn't contain a GDI+ implementation either. The documentation is pretty useless as it is.Gunmaker
@Gunmaker 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.Southerland
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] pdfsharp.net/wiki/PrivateFonts-sample.ashx [2] pdfsharp.net/wiki/FontResolver-sample.ashxRuche
@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.Southerland

© 2022 - 2024 — McMap. All rights reserved.