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使用相对更方便。

参考文献:

1https://github.com/ArtifexSoftware/Ghostscript.NET

2https://blog.csdn.net/gitblog_00044/article/details/138558845

3https://github.com/ArtifexSoftware/Ghostscript.NET/blob/master/Ghostscript.NET.Viewer/FMain.cs

4https://ghostscript.com/releases/gsdnld.html

相关推荐
SamChan9014 小时前
Python 处理小语种 PDF 的编码坑:重音字符、连字与 (cid:xx) 乱码的解决方案
python·ai·pdf·机器翻译
微咣科技16 小时前
平台化工业AI再突破|大捷智能携手岚图汽车,打造车身焊装智能设计行业标杆
pdf·音视频
我滴老baby16 小时前
工业物联网数据库选型:把计算能力放回第一维度
数据库·人工智能·架构·pdf
SamChan9017 小时前
多栏PDF阅读顺序重建:用Python按坐标聚类还原双栏论文的翻译顺序
python·ai·pdf
浩风祭月18 小时前
ChatGPT上传PDF后漏读图表?文字版、扫描件和图片要分开处理
人工智能·chatgpt·pdf·提示词·办公效率
pengzhang13019 小时前
在线改 PDF 文字,我把三个网站用同一份文件实测了一遍
pdf·工具·pdf编辑·在线免费·niubai.vip
泡海椒20 小时前
告别 iText 繁杂配置:jquick-pdf 极简 PDF 生成实战(零基础上手)
java·开发语言·pdf
Saruka的男朋友2 天前
不上传文件也能转格式?拆解一个纯浏览器端的 EPUB 工具链的设计思路
pdf·epub·eink
asdzx672 天前
Python 实战:基于 Spire.PDF 为 PDF 文档添加自定义文本
python·pdf
SamChan902 天前
PyMuPDF vs pdfplumber vs pypdf:PDF 文本提取实测对比(翻译预处理视角)
python·ai·pdf