现在的位置: 首页 > 编程语言 > 正文

如何使用 GhostScript 在 ASP.NET Web应用程序中显示PDF

2020年06月04日 编程语言 ⁄ 共 3053字 ⁄ 字号 评论关闭

  原来是以为在Web应用程序中显示PDF是很难的一件事,但现在我可以告诉你们,解决这个问题只需要5分钟的时间。下面学步园小编来讲解下如何使用GhostScript在ASP.NETWeb应用程序中显示PDF?

  如何使用GhostScript在ASP.NETWeb应用程序中显示PDF

  以下这个范例就是一个简单的ASP.NET程序,包含一个网页以及一个IHttpHandler来显示图片,为了让范例尽可能简单,我这边只用了纯粹的服务器端代码而不使用任何浏览器客户端代码。

  开始

  首先你将需要Cyotek.GhostScript与Cyotek.GhostScript.PdfConversion.zip

  定位gsdll32.dll

  为了能让程序正常运行,gsdll32.dll需要放置在你的应用程序路径下。可能在你的Windows32位系统的system32文件夹,也可能在Windows64位系统的SysWOW64文件夹。

  当在开发过程中,我总是让网站根目录下的bin目录存放这个dll文件。当然因为我测试本范例时都是使用自己的机器,所以在IIS的FullTrust信任级别下是正常运行的,但是可不保证在MediumTrust或者更低的信任级别下能正常工作。

  必须运行在64位Windows系统

  你有如下选择:

  创建与64位系统对应的GhostScript.dll.

  使用IIS7.0或更高的版本

  创建解决方案

  1,先创建一个ASP.NETWeb应用程序。然后打开Default.aspx添加以下代码

  01 <%@PageLanguage="C#"AutoEventWireup="true"   02 CodeBehind="Default.aspx.cs"Inherits="GhostScriptWebTest._Default"%>

  03

  04

  06

  07

  08

  09

  10

  11

  12

  13

  14

  15

  17

  19

  20

  21

  23

  24

  25

  26

  27

  整段代码应该很好理解,唯一有意思的就是那个pdfImage的Image控件,此控件会调用一个Httphandler,也就是我下一部分将讲解的内容。

  请注意到pdfImage控件是指向到一个sample.pdf文件的,所以你在向网站的根目录添加任意pdf文件时请确保pdf文件的BuildAction被设置为Content,否则会无效。

  创建Imagehandler

  如何配合IHttpHandler创建图像处理的问题你在网络上可以找到一大堆技术文章,所以我这里就不多说了。注意我下面的代码是以添加了GhostScript.dll作引用为前提的。

  01 usingSystem;

  02 usingSystem.Drawing;

  03 usingSystem.Drawing.Imaging;

  04 usingSystem.Web;

  05 usingCyotek.GhostScript.PdfConversion;

  06

  07 namespaceGhostScriptWebTest

  08 {

  09 publicclassPdfImage:IHttpHandler

  10 {

  11 publicvoidProcessRequest(HttpContextcontext)

  12 {

  13 stringfileName;

  14 intpageNumber;

  15 Pdf2Imageconvertor;

  16 Bitmapimage;

  17

  18 fileName=context.Server.MapPath("~/"+context.Request.QueryString["fileName"]);

  19 pageNumber=Convert.ToInt32(context.Request.QueryString["page"]);

  20

  21 //converttheimage

  22 convertor=newPdf2Image(fileName);

  23 image=convertor.GetImage(pageNumber);

  24

  25 //setthecontenttype

  26 context.Response.ContentType="image/png";

  27

  28 //savetheimagedirectlytotheresponsestream

  29 image.Save(context.Response.OutputStream,ImageFormat.Png);

  30 }

  31

  32 publicboolIsReusable

  33 {get{returntrue;}}

  34 }

  35 }

  如何使用GhostScript在ASP.NETWeb应用程序中显示PDF

  恩,这也是一段很简单的代码。先通过查询字符串来提取pdf的文件名以及页码索引,然后创建一个Pdf2Image的对象,从pdf文件的指定页中获取图像。

  接下来,你需要设置Response的ContentType对象来让浏览器知道该如何处理。最后我们将图片的流对象直接存储到Response的OutputStream属性中。

  简单的导航

  现在我们能显示PDF了,但是还需要一些基本的导航。打开Default.aspx.cs然后添加以下代码

  viewsourceprint?

  01 usingSystem;

  02 usingSystem.Collections.Specialized;

  03 usingSystem.Web;

  04 usingCyotek.GhostScript.PdfConversion;

  05

  06 namespaceGhostScriptWebTest

  07 {

  08 publicpartialclass_Default:System.Web.UI.Page

  09 {

  10 protectedvoidpreviousLinkButton_Click(objectsender,EventArgse)

  11 {

  12 this.IncrementPage(-1);

  13 }

  14

  15 protectedvoidnextLinkButton_Click(objectsender,EventArgse)

  16 {

  17 this.IncrementPage(1);

  18 }

  19

  20 privatevoidIncrementPage(intincrement)

  21 {

  22 NameValueCollectionqueryString;

  23 intpageNumber;

  24 stringpdfFileName;

  25 Pdf2Imageconverter;

  26

  27 queryString=HttpUtility.ParseQueryString(pdfImage.ImageUrl.Substring(pdfImage.ImageUrl.IndexOf("?")));

  28 pdfFileName=queryString["fileName"];

  29 pageNumber=Convert.ToInt32(queryString["page"])+increment;

  30 converter=newPdf2Image(this.Server.MapPath("~/"+pdfFileName));

  31

  32 if(pageNumber>0&&pageNumber<=converter.PageCount)   33 pdfImage.ImageUrl=string.Format("~/PdfImage.ashx?fileName={0}&page={1}",pdfFileName,pageNumber);   34 }   35 }   36 }   很明显,只需要通过更改页码我们就能得到需要的成果了。   以上就是关于“如何使用GhostScript在ASP.NETWeb应用程序中显示PDF”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!

抱歉!评论已关闭.