问题描述
我正在使用iTextSharp生成动态PDF文档。我需要使用一个非常具体的字体,我有许可的.ttf文件。
I am using iTextSharp to generate dynamic PDF documents. I have a requirement to use a very specific font for which I have the licensed .ttf file.
我可以使用下面的代码加载和使用字体,但是我更喜欢将字体文件定位为我的类库中的嵌入式资源,而不是依赖于磁盘上的特定位置。
I can use the code the below to load and use the font, however I would much prefer to have the font file as located as an embedded resource in my class library rather than being reliant on a specific location on disk.
string fontpath = Server.MapPath(".");
BaseFont customfont = BaseFont.CreateFont(fontpath + "myspecial.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
Font font = new Font(customfont, 12);
string s = "My expensive custom font.";
doc.Add(new Paragraph(s, font));
有人可以帮我解决这个问题吗?
Can anybody help me as to how I might achieve this?
推荐答案
在查看ITextSharp源代码后,您似乎可以使用以下 BaseFont.CreateFont 的重载将您的嵌入式资源用作字体(来自):
After reviewing the ITextSharp source it looks like you may be able to use the following overload of BaseFont.CreateFont
to use your embedded resource as a font (line 543 from BaseFont.cs):
public static BaseFont CreateFont(String name, String encoding, bool embedded, bool cached, byte[] ttfAfm, byte[] pfb)
ttfAfm
应该将TTF文件表示为 byte []
。
ttfAfm
should represent the TTF file as a byte[]
.
这篇关于从iTextSharp中的嵌入式资源加载BaseFont的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!