2 Star 5 Fork 3

OY/MyFile

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
SimFontResolver.cs 3.45 KB
一键复制 编辑 原始数据 按行查看 历史
OY 提交于 2024-05-20 16:52 +08:00 . 提交demo
using PdfSharp.Fonts; using System.Reflection; namespace pdfsharpDemo;/// <summary>/// 中文字体解析器/// </summary>public class ChineseFontResolver : IFontResolver{ /// <summary> /// 字体作为嵌入资源所在程序集 /// </summary> public static string FontAssemblyString { get; set; } = "pdfsharpDemo"; /// <summary> /// 字体作为嵌入资源所在命名空间 /// </summary> public static string FontNamespace { get; set; } = "pdfsharpDemo.Fonts"; /// <summary> /// 字体名称 /// </summary> public static class FamilyNames { // This implementation considers each font face as its own family. /// <summary> /// 仿宋 /// </summary> public const string SIMFANG = "simfang.ttf"; /// <summary> /// 黑体 /// </summary> public const string SIMHEI = "simhei.ttf"; /// <summary> /// 楷书 /// </summary> public const string SIMKAI = "simkai.ttf"; /// <summary> /// 隶书 /// </summary> public const string SIMLI = "simli.ttf"; /// <summary> /// 宋体 /// </summary> public const string SIMSUN = "simsun.ttf"; /// <summary> /// 宋体加粗 /// </summary> public const string SIMSUNB = "simsunb.ttf"; /// <summary> /// 幼圆 /// </summary> public const string SIMYOU = "simyou.ttf"; } /// <summary> /// Selects a physical font face based on the specified information /// of a required typeface. /// </summary> /// <param name="familyName">Name of the font family.</param> /// <param name="isBold">Set to <c>true</c> when a bold font face /// is required.</param> /// <param name="isItalic">Set to <c>true</c> when an italic font face /// is required.</param> /// <returns> /// Information about the physical font, or null if the request cannot be satisfied. /// </returns> public FontResolverInfo? ResolveTypeface(string familyName, bool isBold, bool isItalic) { // Note: PDFsharp calls ResolveTypeface only once for each unique combination // of familyName, isBold, and isItalic. return new FontResolverInfo(familyName, isBold, isItalic); // Return null means that the typeface cannot be resolved and PDFsharp forwards // the typeface request depending on PDFsharp build flavor and operating system. // Alternatively forward call to PlatformFontResolver. //return PlatformFontResolver.ResolveTypeface(familyName, isBold, isItalic); } /// <summary> /// Gets the bytes of a physical font face with specified face name. /// </summary> /// <param name="faceName">A face name previously retrieved by ResolveTypeface.</param> /// <returns> /// The bits of the font. /// </returns> public byte[]? GetFont(string faceName) { // Note: PDFsharp never calls GetFont twice with the same face name. // Note: If a typeface is resolved by the PlatformFontResolver.ResolveTypeface // you never come here. var name = $"{FontNamespace}.{faceName}"; using Stream stream = Assembly.Load(FontAssemblyString).GetManifestResourceStream(name) ?? throw new ArgumentException("No resource named '" + name + "'."); int num = (int)stream.Length; byte[] array = new byte[num]; stream.Read(array, 0, num); // Return the bytes of a font. return array; }}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/huangguishen/MyFile.git
git@gitee.com:huangguishen/MyFile.git
huangguishen
MyFile
MyFile
master

搜索帮助