PDFSharp WPF 1.32专用字体不适用于Azure Blob存储 [英] PDFSharp WPF 1.32 private fonts will not work with Azure Blob storage

查看:466
本文介绍了PDFSharp WPF 1.32专用字体不适用于Azure Blob存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用PDFSharp将文本添加到PDF文件的代码。如果字体位于本地文件系统上,这种方式会成功运行,但当我向存储在Azure博客存储上的字体提供url时,该方法无效。我也尝试在他们的CDN上使用谷歌字体,这也没有工作。从网址加载字体有问题吗?



示例代码如下。请注意,我已经解决了Azure blob存储上的 CORS问题,所以它不是

  private readonly XPdfFontOptions _fontOptions = 
new XPdfFontOptions(PdfFontEncoding.Unicode,PdfFontEmbedding.Always);
private static readonly XPrivateFontCollection PrivateFontCollection =
XPrivateFontCollection.Global;
$ b $测试
公共异步任务Test01RawPdfSharp()
{
// SETUP
var account =labelapptest;
var fs = new FileStore(account);
var fontUrl =
http://labelapptest.blob.core.windows.net/designernospamcom/justyna sokolowska - erazm-regular.otf;
var pdfUrl =
https://labelapptest.blob.core.windows.net/label-highrez-blank/blank-label00001.pdf;
var fontFamily =Erazm Regular;
var localFontPath = Path.Combine(TestFileHelpers.GetTestDataFileDirectory(@\TestFonts),
Justyna Sokolowska - Erazm-Regular.otf);

// ATTEMPT
var sFontFamilyname =./#+ fontFamily;
var uri = new Uri(fontUrl);
PrivateFontCollection.Add(uri,sFontFamilyname);

var readDirStatus =等待fs.GetDirHandleAsync(FileStorageTypes.LabelHighRezBlank);
using(var readStream = new MemoryStream())
{
await readDirStatus.Result.ReadFileAsync(readStream,pdfUrl);
readStream.Seek(0,SeekOrigin.Begin);
var document = PdfReader.Open(readStream);
var gfx = XGraphics.FromPdfPage(document.Pages [0]);

var font = new XFont(fontFamily,new XUnit(12,XGraphicsUnit.Point),XFontStyle.Regular,_fontOptions);
var brush = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Red));
gfx.DrawString(Hello world,font,brush,10,10);

//验证



$ p在上面的例子中如果我在uri中使用 fontUrl 来添加字体,那么代码在创建XFont(从底部起第六个)的行失败。如果另一方面我使用 localFontPath ,那么它就可以工作了。



错误是:在XFont.cs中的PdfSharp.Drawing.XFont.Initialize()行处不能得到匹配的字形字体'Erazm Regular'



注意:我已下载并编辑1.32 PDFSharp代码,以取代 Debugger.Break 语句,此时有一个例外。 NuGet版本有一个 Debugger.Break 这会导致在发布的代码中挂起 - 参见我的问题关于这个问题。
$ b $ h


$ p最后我换成了PDFsharp WPF 1.50测试版,因为它的字体处理好得多,没有得到任何地方通过http链接加载字体的问题。似乎工作得很好。

解决方案调用 PrivateFontCollection.Add(uri,sFontFamilyname)只是将URI传递给新的System.Windows.Media.FontFamily(baseUri,familyName)。我不知道Media.FontFamily是否可以处理Azure存储的URI。



我们使用PrivateFontCollection和资源中的字体 - 从资源中加载字体是唯一的样本在 FontFamily构造函数(Uri,String)的帮助中。这是已知与PDFsharp一起工作的URI类型。


$ b

FontFamily构造函数(Uri,String)的语法有点神秘,最小的错误会导致找不到。

I have some code that adds text using a private font to a PDF file using PDFSharp. This works successfully if the font is located on the local file system but does not work when I provide it with a url to a font stored on a Azure blog storage. I also tried using a google font on their CDN, which also didn't work. Is there some problem with loading fonts from a web url?

The sample code is below. Note that I have solved the CORS issue on Azure blob storage so it isn't that.

private readonly XPdfFontOptions _fontOptions = 
    new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
private static readonly XPrivateFontCollection PrivateFontCollection =
    XPrivateFontCollection.Global;

[Test]
public async Task Test01RawPdfSharp()
{
    //SETUP 
    var account = "labelapptest";
    var fs = new FileStore(account);
    var fontUrl =
        "http://labelapptest.blob.core.windows.net/designernospamcom/justyna sokolowska - erazm-regular.otf";
    var pdfUrl =
        "https://labelapptest.blob.core.windows.net/label-highrez-blank/blank-label00001.pdf";
    var fontFamily = "Erazm Regular";
    var localFontPath = Path.Combine(TestFileHelpers.GetTestDataFileDirectory(@"\TestFonts"),
        "Justyna Sokolowska - Erazm-Regular.otf");

    //ATTEMPT
    var sFontFamilyname = "./#" + fontFamily;
    var uri = new Uri(fontUrl);
    PrivateFontCollection.Add(uri, sFontFamilyname);

    var readDirStatus = await fs.GetDirHandleAsync(FileStorageTypes.LabelHighRezBlank);
    using (var readStream = new MemoryStream())
    {
        await readDirStatus.Result.ReadFileAsync(readStream, pdfUrl);
        readStream.Seek(0, SeekOrigin.Begin);
        var document = PdfReader.Open(readStream);
        var gfx = XGraphics.FromPdfPage(document.Pages[0]);

        var font = new XFont(fontFamily, new XUnit(12, XGraphicsUnit.Point), XFontStyle.Regular, _fontOptions);
        var brush = new XSolidBrush(XColor.FromKnownColor(XKnownColor.Red));
        gfx.DrawString("Hello world", font, brush, 10, 10);
    }
    //VERIFY
}    

In the example above if I use the fontUrl in the uri for adding the font then the code fails at the line where the XFont is created (6th from bottom). If on the other hand I use the localFontPath then it works.

The error is: Cannot get a matching glyph typeface for font 'Erazm Regular' at line at PdfSharp.Drawing.XFont.Initialize() in XFont.cs: line 234.

NOTE: I have downloaded and edited the 1.32 PDFSharp code to replace the Debugger.Break statement with an exception at this point. The NuGet version has a Debugger.Break which causes a hang in released code - see my SO question about that issue.

UPDATE

In the end I swapped to PDFsharp WPF 1.50 beta as its font handling is much better and I wasn't getting anywhere with the problem of loading a font via a http link. Seems to be working really well.

解决方案

The call to PrivateFontCollection.Add(uri, sFontFamilyname) just passes the URI to new System.Windows.Media.FontFamily(baseUri, familyName). I don't know whether Media.FontFamily can handle URIs for Azure storage.

We use the PrivateFontCollection with fonts from resources - and loading fonts from resources is the only sample given in the help for FontFamily Constructor (Uri, String). That's the type of URI that is known to work with PDFsharp.

The syntax for FontFamily Constructor (Uri, String) is somewhat cryptic and the smallest error will lead to "not found".

这篇关于PDFSharp WPF 1.32专用字体不适用于Azure Blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆