2016-05-23 36 views
0

我只是用在一個藍色的WebApplication網站Arial字體,但是當我到達這條線:如何在我的網站的PDFsharp中使用字體?

MainFont = new XFont("Arial", FontSize); 

它拋出一個異常讀數:Font data could not retrieved.

我還以爲Arial字體將被安裝在服務器上...我也嘗試將其更改爲Sans-Serif以匹配Microsoft生成的Web站點的默認字體......但仍然失敗。

我也嘗試將Arial.ttf添加到項目中,但那沒有奏效。

回答

0

感謝指針@PDFSharp團隊。這裏是我的PdfSharp 1.5 beta3b實現:

添加你想你的項目的字體 - 在我下面的例子,我把ArialMyProject\fonts\arial\arial.ttf等設置每個字體文件作爲嵌入資源(屬性 - >生成操作)。

應用的字體解析只有一次使用這樣的靜態調用:

MyFontResolver.Apply(); // Ensures it's only applied once 

這裏的字體解析類:

class MyFontResolver : IFontResolver 
{ 
    public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic) 
    { 
     // Ignore case of font names. 
     var name = familyName.ToLower().TrimEnd('#'); 

     // Deal with the fonts we know. 
     switch (name) 
     { 
      case "arial": 
       if (isBold) 
       { 
        if (isItalic) 
         return new FontResolverInfo("Arial#bi"); 
        return new FontResolverInfo("Arial#b"); 
       } 
       if (isItalic) 
        return new FontResolverInfo("Arial#i"); 
       return new FontResolverInfo("Arial#"); 
     } 

     // We pass all other font requests to the default handler. 
     // When running on a web server without sufficient permission, you can return a default font at this stage. 
     return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic); 
    } 

    /// <summary> 
    /// Return the font data for the fonts. 
    /// </summary> 
    public byte[] GetFont(string faceName) 
    { 
     switch (faceName) 
     { 
      case "Arial#": 
       return FontHelper.Arial; 

      case "Arial#b": 
       return FontHelper.ArialBold; 

      case "Arial#i": 
       return FontHelper.ArialItalic; 

      case "Arial#bi": 
       return FontHelper.ArialBoldItalic; 
     } 

     return null; 
    } 


    internal static MyFontResolver OurGlobalFontResolver = null; 

    /// <summary> 
    /// Ensure the font resolver is only applied once (or an exception is thrown) 
    /// </summary> 
    internal static void Apply() 
    { 
     if (OurGlobalFontResolver == null || GlobalFontSettings.FontResolver == null) 
     { 
      if (OurGlobalFontResolver == null) 
       OurGlobalFontResolver = new MyFontResolver(); 

      GlobalFontSettings.FontResolver = OurGlobalFontResolver; 
     } 
    } 
} 


/// <summary> 
/// Helper class that reads font data from embedded resources. 
/// </summary> 
public static class FontHelper 
{ 
    public static byte[] Arial 
    { 
     get { return LoadFontData("MyProject.fonts.arial.arial.ttf"); } 
    } 

    public static byte[] ArialBold 
    { 
     get { return LoadFontData("MyProject.fonts.arial.arialbd.ttf"); } 
    } 

    public static byte[] ArialItalic 
    { 
     get { return LoadFontData("MyProject.fonts.arial.ariali.ttf"); } 
    } 

    public static byte[] ArialBoldItalic 
    { 
     get { return LoadFontData("MyProject.fonts.arial.arialbi.ttf"); } 
    } 

    /// <summary> 
    /// Returns the specified font from an embedded resource. 
    /// </summary> 
    static byte[] LoadFontData(string name) 
    { 
     var assembly = Assembly.GetExecutingAssembly(); 

     // Test code to find the names of embedded fonts 
     //var ourResources = assembly.GetManifestResourceNames(); 

     using (Stream stream = assembly.GetManifestResourceStream(name)) 
     { 
      if (stream == null) 
       throw new ArgumentException("No resource with name " + name); 

      int count = (int)stream.Length; 
      byte[] data = new byte[count]; 
      stream.Read(data, 0, count); 
      return data; 
     } 
    } 
} 

這是基於這兩個單一的,完整的和工人階級幾乎相同的帖子:this blogthis forum

0

使用最新版本的PDFsharp(目前是1.50 beta 3)並實現IFontResolver。

參見:
https://stackoverflow.com/a/32489271/162529
https://stackoverflow.com/a/29059207/162529

字體可以在服務器上安裝,但PDFsharp無法讀取它。

+0

看看這些鏈接中的代碼,它看起來像我仍然需要將字體文件附加到我的項目,即使它可能存在於服務器上 - 是否正確? – noelicus

+0

PDFsharp必須能夠訪問字體文件。如果它駐留在服務器上的受保護系統文件夾中,那麼它仍然無法到達。使用PDFsharp 1.50,您需要一個字節數組,因此您可以將其作爲資源添加到您的程序集或作爲內容文件添加到您的項目中。 –

相關問題