C#测试调用Ghostscript.NET浏览PDF文件

Ghostscript.NET是针对Ghostscript的C#封装库,支持解析PostScript语言、操作PDF文件等。使用Ghostscript.NET的GhostscriptViewer 模块可以以图片形式查看PDF文档。本文学习并测试调用Ghostscript.NET模块打开及浏览PDF文件的基本用法。
  Ghostscript.NET目前主要支持.net framework,使用VS2022创建基于.net framework 4.8的Winform项目,在Nuget包管理器中搜索并安装Ghostscript.NET包。同时还需在参考文献4中下载相应32位或者64位的依赖包,否则程序运行过程中会报下面截图错误,提示需下载依赖安装包。


  Ghostscript.NET的GitHub官网中提供了GhostscriptViewer的使用案例(参考文献3),主要是配合PictureBox控件使用,调用GhostscriptViewer的Open函数打开PDF文件,然后处理GhostscriptViewer的DisplayPag、DisplaySize、DisplayUpdate事件,更新PictureBox控件的显示图片。同时,调用GhostscriptViewer的ShowFirstPage、ShowLastPage、ShowPreviousPage、ShowNextPage函数切换当前显示图片。
  主要代码包括(从参考文献3的示例中简化而来):

csharp 复制代码
private GhostscriptViewer _viewer;

public Form1()
{
	...
	...
    _viewer = new GhostscriptViewer();
    _viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
    _viewer.DisplayUpdate += new GhostscriptViewerViewEventHandler(_viewer_DisplayUpdate);
    _viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
}
 void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
 {
     pbPage.Image = e.Image;
 }

 void _viewer_DisplayUpdate(object sender, GhostscriptViewerViewEventArgs e)
 {
     pbPage.Invalidate();
     pbPage.Update();
 }

 void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
 {
     pbPage.Invalidate();
     pbPage.Update();
 }
private void btnFirst_Click(object sender, EventArgs e)
{
    _viewer.ShowFirstPage();
}

private void btnLast_Click(object sender, EventArgs e)
{
    _viewer.ShowLastPage();
}

private void btnPre_Click(object sender, EventArgs e)
{
    _viewer.ShowPreviousPage();
}

private void btnNext_Click(object sender, EventArgs e)
{
    _viewer.ShowNextPage();
}

下图为程序运行效果。Ghostscript.NET与PdfiumViewer相比,两者的Nuget包安装及依赖包安装类似,但后者提供有简单的浏览控件,且支持.net core。因此两者相比,PdfiumViewer使用相对更方便。

参考文献:

1\]https://github.com/ArtifexSoftware/Ghostscript.NET \[2\]https://blog.csdn.net/gitblog_00044/article/details/138558845 \[3\]https://github.com/ArtifexSoftware/Ghostscript.NET/blob/master/Ghostscript.NET.Viewer/FMain.cs \[4\]https://ghostscript.com/releases/gsdnld.html

相关推荐
月巴月巴白勺合鸟月半3 小时前
一次PDF文件的处理(一)
pdf·c#
弓.长.4 小时前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-pdf — PDF文档查看器
react native·pdf·harmonyos
开开心心就好6 小时前
绿色版PDF多功能工具,支持编辑转换
人工智能·windows·pdf·ocr·excel·语音识别·harmonyos
月巴月巴白勺合鸟月半11 小时前
一次PDF文件的处理(二)
pdf·c#
开开心心就好11 小时前
免费无广告的礼金记账本,安卓应用
java·前端·ubuntu·edge·pdf·负载均衡·语音识别
优化控制仿真模型12 小时前
2015-2025年12月英语六级历年真题及答案PDF电子版(含听力音频)
经验分享·pdf
爱编程的小吴12 小时前
LangChain基础入门:DocumentLoader加载PDF/Markdown文档实战
python·langchain·pdf
软件工程小施同学12 小时前
区块链论文速读 CCF A--CCS 2025 (1) 附pdf下载
pdf·区块链
葡萄城技术团队1 天前
SpreadJS 中“打印”和“导出 PDF”到底该选哪个?
pdf
优化控制仿真模型1 天前
2025年12月英语六级真题及答案解析完整版(第一、二、三套全PDF)
经验分享·pdf