PdfSharp 创建编辑 PDF 文件,.NET 跨平台 PDF 处理控件

2023年7月20日 1559点热度 1人点赞 0条评论
内容纲要

PdfSharp 是一个 .NET 跨平台的 PDF 处理框架,PdfSharp 使用的是 MIT 开源协议,无论是个人还是商业使用均可,且不限制闭源或开源。PdfSharp 由社区维护,无任何收费购买项目。

笔者看中它:

  • 体积轻小,
  • 操作简单,
  • 跨平台,不会出现 System.Drawing 报错,
  • 完全免费,MIT 开源协议

file

命名空间:

using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Snippets.Font;

首先编写一个自定义字体导入类型:

官方默认有个 SegoeWpFontResolver 提供了系统字体,所以我们在 SegoeWpFontResolver 之外包一层即可,如果由自定义字体,则返回自定义字体,否则使用默认的字体查找逻辑。

    public class MyFontResplver : IFontResolver
    {
        private readonly Dictionary<string, XFontSource> _fonts = new();
        private readonly SegoeWpFontResolver _baseResolver = new SegoeWpFontResolver();
        public string AddFont(string fileName)
        {
            XFontSource fontSource = XFontSource.GetOrCreateFrom(File.ReadAllBytes(fileName));
            _fonts.Add(fontSource.FontName, fontSource);
            return fontSource.FontName;
        }

        public byte[]? GetFont(string faceName)
        {
            if (_fonts.TryGetValue(faceName, out var xFontSource))
            {
                return xFontSource.Bytes;
            }
            return _baseResolver.GetFont(faceName);
        }

        public FontResolverInfo? ResolveTypeface(string familyName, bool isBold, bool isItalic)
        {
            if (_fonts.TryGetValue(familyName, out var xFontSource))
            {
                return new FontResolverInfo(familyName, isBold, isItalic);
            }

            return _baseResolver.ResolveTypeface(familyName, isBold, isItalic);
        }
    }

然后创建新的 PDF 文件,创建页并编辑:

使用 new PdfDocument();new PdfDocument("my.pdf"); 都是重新创建文件,而不是在源 pdf 上操作。这样会导致将源 pdf 文件清空的。正确的编辑方法请看后面。

    static void Main()
    {
        // 加载自定义字体文件
        var fontResplover = new MyFontResplver();

        GlobalFontSettings.FontResolver = fontResplover;
        var fontName = fontResplover.AddFont("my.ttf");

        var document = new PdfDocument();
        document.Info.Title = "标题测试";
        document.Info.Subject = "测试";

        // 插入一个页面
        PdfPage page = document.AddPage();

        // 图形绘制对象
        XGraphics gfx = XGraphics.FromPdfPage(page);

        // 画线
        var width = page.Width;
        var height = page.Height;
        gfx.DrawLine(XPens.Red, 0, 0, width, height);
        gfx.DrawLine(XPens.Red, width, 0, 0, height);

        // 画图形
        var r = width / 5;
        gfx.DrawEllipse(new XPen(XColors.Red, 1.5), XBrushes.White, new XRect(width / 2 - r, height / 2 - r, 2 * r, 2 * r));

        // 写文字
        var font = new XFont(fontName, 20, XFontStyleEx.BoldItalic);

        gfx.DrawString("测试", font, XBrushes.Black,
            new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);

        var filename = "new.pdf";
        document.Save(filename);
    }

如果需要在旧的 pdf 上新增页,可以这样导入 pdf 文件:

        var document = PdfReader.Open("测试.pdf");

如果是新增页编辑,则使用:

        PdfPage page = document.AddPage();

如果是使用之前的页面编辑:

        PdfPage page = document.Pages[0];

痴者工良

高级程序员劝退师

文章评论