使用自定义的字体MVCRazorToPdf(iTextSharp的) [英] MVCRazorToPdf (iTextSharp) using custom font

查看:709
本文介绍了使用自定义的字体MVCRazorToPdf(iTextSharp的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的NuGet包MVCRazorToPdf自定义字体添加到我的PDF输出,但我有如何做到这一点,作为iTextSharp的文档麻烦的是不是伟大,一切似乎已经过时。

I am trying to add a custom font to my pdf output using the nuget package MVCRazorToPdf but I am having trouble with how to do this as the documentation for iTextSharp isn't great and all seems to be outdated.

目前的代码我有创建PDF是:

The current code I have for creating the pdf is:

return new PdfActionResult(
    "test.cshtml", 
    new TestModel(),
    (writer, document) =>
    {
        FontFactory.Register(HostingEnvironment.MapPath("~/content/fonts/vegur-regular-webfont.ttf"), "VegurRegular");
    });



其中,作家 PdfWriter 文件文件

这是你需要使用使用 FontFactory 显示的所有实例 XmlWorker ,但我不'T有机会获得这一点,所以我想知道是否有任何方法来改变文件使用作家字体文件

All the examples of using the FontFactory show that you need to use the XmlWorker but I don't have access to that, so I was wondering if there was any way to change the documents font using the writer or document?

我已经看到了还有就是 document.HtmlStyleClass 属性,但找不到任何有关如何使用此的任何地方。

I've seen that there is the document.HtmlStyleClass property but can't find anything about how to use this anywhere.

任何帮助,将不胜感激。

Any help with this would be greatly appreciated

推荐答案

MVCRazorToPdf 是一个非常,周围iTextSharp的是非常简单的包装 XMLWorker ,并使用更简单 XMLWorkerHelper 与所有默认设置来完成工作。如果你看一下你会看到这一点:

MVCRazorToPdf is a very, very simple wrapper around iTextSharp's XMLWorker and uses the even simpler XMLWorkerHelper with all defaults to do its work. If you look at the source you'll see this:

                document.Open();


                using (var reader = new StringReader(RenderRazorView(context, viewName)))
                {
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);

                    document.Close();
                    output = workStream.ToArray();
                }

如果你在使用的NuGet版本,那么你就死定了集坚持这个实现,你不会是能够注册一个自定义的字体。

If you're dead-set on using the NuGet version then you're stuck with this implementation and you're not going to be able to register a custom font.

不过,有一个的悬而未决的问题关于这一点,的包括一个修复所以,如果你愿意从源代码编译你可以应用的变化,你应该准备就绪

However, there's an open issue regarding this that includes a fix so if you're willing to compile from source you can apply that change and you should be all set.

如果你想多走一步我建议你阅读的这个伟大的帖子显示与iTextSharp的简单解析HTML是如何以及布鲁诺的帖子这里显示如何注册字体。

If you want to go one step further I'd recommend reading this great post that shows how simple parsing HTML with iTextSharp is as well Bruno's post here that shows how to register fonts.

修改

按照帖子中包含一个固定链接(以防万一将来的链接断裂),用语句更改上面的来:

As per the post in the includes a fix link (just in case the link breaks in future), change the above using statement to:

        using (var reader = new MemoryStream(Encoding.UTF8.GetBytes(RenderRazorView(context, viewName))))
        {
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader, null, FontFactory.FontImp as IFontProvider);

            document.Close();
            output = workStream.ToArray();
        }

和再挂号了问题的字体出厂时使用上述将工作风格=FONT-FAMILY:VegurRegular;

And then the font factory as registered in the question above will work when using style="font-family:VegurRegular;"

这篇关于使用自定义的字体MVCRazorToPdf(iTextSharp的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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