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

原来是以为在Web应用程序中显示PDF是很难的一件事,但现在我可以告诉你们,解决这个问题只需要5分钟的时间。

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

 

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

定位 gsdll32.dll

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

 

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

 

必须运行在 64 位 Windows 系统

你有如下选择:

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

使用IIS7.0或更高的版本

 

创建解决方案

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

01 <%@ Page Language="C#" AutoEventWireup="true"

 

02    CodeBehind="Default.aspx.cs" Inherits="GhostScriptWebTest._Default" %>

 

03  

 

04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 

05    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

06  

 

07 <html xmlns="http://www.w3.org/1999/xhtml">

 

08 <head runat="server">

 

09     <title>PDF Conversion Example</title>

 

10 </head>

 

11 <body>

 

12     <form id="form1" runat="server">

 

13     <div>

 

14         <p>

 

15             <asp:LinkButton runat="server" ID="previousLinkButton"

 

16                Text="Previous" OnClick="previousLinkButton_Click" />

 

17             <asp:LinkButton runat="server" ID="nextLinkButton"

 

18                Text="Next" OnClick="nextLinkButton_Click" />

 

19         </p>

 

20         <p>

 

21             <asp:Image runat="server" ID="pdfImage"

 

22                ImageUrl="~/PdfImage.ashx?fileName=sample.pdf&page=1" />

 

23         </p>

 

24     </div>

 

25     </form>

 

26 </body>

 

27 </html>

 

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

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

 

创建 Image handler

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

01 using System;

 

02 using System.Drawing;

 

03 using System.Drawing.Imaging;

 

04 using System.Web;

 

05 using Cyotek.GhostScript.PdfConversion;

 

06  

 

07 namespace GhostScriptWebTest

 

08 {

 

09   public class PdfImage : IHttpHandler

 

10   {

 

11     public void ProcessRequest(HttpContext context)

 

12     {

 

13       string fileName;

 

14       int pageNumber;

 

15       Pdf2Image convertor;

 

16       Bitmap image;

 

17  

 

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

 

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

 

20  

 

21       // convert the image

 

22       convertor = new Pdf2Image(fileName);

 

23       image = convertor.GetImage(pageNumber);

 

24  

 

25       // set the content type

 

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

 

27  

 

28       // save the image directly to the response stream

 

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

 

30     }

 

31  

 

32     public bool IsReusable

 

33     { get { return true; } }

 

34   }

 

35 }

 

 

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

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

 

简单的导航

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

view sourceprint?

01 using System;

 

02 using System.Collections.Specialized;

 

03 using System.Web;

 

04 using Cyotek.GhostScript.PdfConversion;

 

05  

 

06 namespace GhostScriptWebTest

 

07 {

 

08   public partial class _Default : System.Web.UI.Page

 

09   {

 

10     protected void previousLinkButton_Click(object sender, EventArgs e)

 

11     {

 

12       this.IncrementPage(-1);

 

13     }

 

14  

 

15     protected void nextLinkButton_Click(object sender, EventArgs e)

 

16     {

 

17       this.IncrementPage(1);

 

18     }

 

19  

 

20     private void IncrementPage(int increment)

 

21     {

 

22       NameValueCollection queryString;

 

23       int pageNumber;

 

24       string pdfFileName;

 

25       Pdf2Image converter;

 

26  

 

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

 

28       pdfFileName = queryString["fileName"];

 

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

 

30       converter = new Pdf2Image(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 }

 

 

很明显,只需要通过更改页码我们就能得到需要的成果了。

原文链接 :http://www.codeproject.com/Articles/421645/Displaying-the-contents-of-a-PDF-file-in-an-ASP-NE
oschina 原创翻译

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Grow your business fast with

Suku