PdfiumViewer组件扩展(Pdfium.Net.Free)--创建字符子集

项目地址:

Pdfium.Net:https://github.com/1000374/Pdfium.Net.Free

PdfiumViewer:https://github.com/1000374/PdfiumViewer

Pdfium.Net.Free 支持

  • .NETFramework 4.0

  • .NETFramework 4.5

  • .NETStandard 2.0

  • .Net8.0

可以和PdfiumViewer.Free共同使用预览、编辑pdf,也可以直接引用Pdfium.Net.Free 操作pdf,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功能等待后续~~

Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--加载字体 草稿

接上篇,怎么创建字符子集呢?获取字符集内的字形符号需要引用wpf下PresentationCore.dll,根据比对传入字符和字符集中的字形获取对应的字形字节,从而生成新的字符集,

如需要生成ttf文件,则直接byte[]保存成ttf即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// <summary>
/// create SubCharacterSet
/// </summary>
/// <param name="fontPath">font path</param>
/// <param name="sourceText"></param>
/// <returns></returns>
public static byte[] CreateSubSet(this string fontPath, string sourceText)
{
    if (!File.Exists(fontPath))
        throw new ArgumentException($"{fontPath} not find");
    var glyphTypeface = new GlyphTypeface(new Uri(fontPath, UriKind.RelativeOrAbsolute));
    var Index = new List<ushort>();
    var sourceTextBytes = Encoding.Unicode.GetBytes(sourceText);
    var sourceTextChars = Encoding.Unicode.GetChars(sourceTextBytes);
    for (var charPos = 0; charPos <= (sourceTextChars.Length - 1); charPos++)
    {
        var sourceTextCharVal = (int)sourceTextChars[charPos];
        var glyphIndex = glyphTypeface.CharacterToGlyphMap[sourceTextCharVal];
        if (!Index.Contains(glyphIndex))
            Index.Add(glyphIndex);
    }
    return glyphTypeface.ComputeSubset(Index);
}

  

 

posted @   小树禾小央  阅读(117)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示