当前位置: 首页 > article >正文

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的示例中简化而来):

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


http://www.kler.cn/news/317356.html

相关文章:

  • <刷题笔记> 二叉搜索树与双向链表注意事项
  • OpenHarmony(鸿蒙南向开发)——标准系统方案之瑞芯微RK3568移植案例(上)
  • 流域碳中和技术
  • 使用Docker一键部署Blossom笔记软件
  • 【C#生态园】一文详解:NHibernate、Entity Framework Core、Dapper 等 .NET ORM 框架优劣对比
  • M9410A VXT PXI 矢量收发信机,300/600/1200MHz带宽
  • 防火墙详解(三)华为防火墙基础安全策略配置(命令行配置)
  • 11. DPO 微调示例:根据人类偏好优化LLM大语言模型
  • 【电商搜索】现代工业级电商搜索技术-Ha3搜索引擎平台简介
  • 应用层-网络协议
  • Java面试篇基础部分- Java中的阻塞队列
  • 解决selenium爬虫被浏览器检测问题
  • 5. 条件 Conditionals
  • 56 mysql 用户权限相关的实现
  • Spring高手之路24——事务类型及传播行为实战指南
  • DHCP中继工作原理
  • 算法【Dijkstra算法及分层图最短路】
  • WPF实现关系图
  • Vue开发前端图片上传给java后端
  • MMD模型一键完美导入UE5-VRM4U插件方案(一)
  • 为什么三星、OPPO、红米都在用它?联发科12nm级射频芯片的深度剖析
  • Fyne ( go跨平台GUI )中文文档-入门(一)
  • Adobe预览今年晚些时候推出的AI视频工具
  • RAG技术全面解析:Langchain4j如何实现智能问答的跨越式进化?
  • 深入理解Vue3中style的scoped
  • 简单计算器(python基础代码撰写)
  • Vue3:具名插槽
  • 微信小程序07-开发进阶
  • c++难点核心笔记(一)
  • 基于SpringBoot+Vue的在线问诊管理系统