.Net PdfiumViewer 打印时无法渲染电子签名问题的解决方法
转载请标明原出处:https://www.cnblogs.com/crpfs/p/18463735
1. 先决条件
本文修改的动态库是从如下的 NuGet 包中获取的:

如果使用的是 Visual Studio 中的 NuGet 包管理器获取的,则其动态库(.dll)一般会存放在:
复制.\packages\PdfiumViewer.2.13.0.0\lib\net20\PdfiumViewer.dll
另外,还需要反编译动态库的工具:dnSpy,本文使用的是 dnSpy 作者弃坑后另一位佬接手重生的项目 dnSpyEx。
2. 解决方法
① 将动态库拖入 dnSpy,展开如下命名空间:

② 找到 PdfPrintDocument 类,展开,再找到 RenderPage 方法:

③ 右键 ==> 修改方法(C#),修改最后一行代码:
复制// 原本的代码
// this._document.Render(page, e.Graphics, e.Graphics.DpiX, e.Graphics.DpiY, new Rectangle(PdfPrintDocument.AdjustDpi((double)e.Graphics.DpiX, left), PdfPrintDocument.AdjustDpi((double)e.Graphics.DpiY, top), PdfPrintDocument.AdjustDpi((double)e.Graphics.DpiX, num3), PdfPrintDocument.AdjustDpi((double)e.Graphics.DpiY, num4)), PdfRenderFlags.ForPrinting | PdfRenderFlags.Annotations);
// 修改后的代码
using (Image image = this._document.Render(page, PdfPrintDocument.AdjustDpi((double)e.Graphics.DpiX, (double)e.PageBounds.Width), PdfPrintDocument.AdjustDpi((double)e.Graphics.DpiX, (double)e.PageBounds.Height), e.Graphics.DpiX, e.Graphics.DpiY, PdfRotation.Rotate0, PdfRenderFlags.ForPrinting | PdfRenderFlags.Annotations))
{
e.Graphics.DrawImageUnscaled(image, e.PageBounds.Location);
}
④ 在菜单栏选择 文件 ==> 保存模块,直接点确定即可重新编译动态库。
注意:会直接覆盖原先的动态库,如需要则注意备份。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章