2KB项目,专业的源码交易网站 帮助 收藏 每日签到

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

  • 时间:2019-01-23 18:43 编辑:2KB 来源:2KB.COM 阅读:349
  • 扫一扫,手机访问
  • 分享
摘要: 英文原文:Dis
英文原文:Displaying the contents of a PDF file in an ASP.NET application using GhostScript 原来是以为在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 添加以下代码

<%@ Page Language="C#" AutoEventWireup="true" 
   CodeBehind="Default.aspx.cs" Inherits="GhostScriptWebTest._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>PDF Conversion Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p>
            <asp:LinkButton runat="server" ID="previousLinkButton" 
               Text="Previous" OnClick="previousLinkButton_Click" />
            <asp:LinkButton runat="server" ID="nextLinkButton" 
               Text="Next" OnClick="nextLinkButton_Click" />
        </p>
        <p>
            <asp:Image runat="server" ID="pdfImage" 
               ImageUrl="~/PdfImage.ashx?fileName=sample.pdf&page=1" />
        </p>
    </div>
    </form>
</body>
</html>


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

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


创建 Image handler

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

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using Cyotek.GhostScript.PdfConversion;

namespace GhostScriptWebTest
{
  public class PdfImage : IHttpHandler
  {
    public void ProcessRequest(HttpContext context)
    {
      string fileName;
      int pageNumber;
      Pdf2Image convertor;
      Bitmap image;

      fileName = context.Server.MapPath("~/" + context.Request.QueryString["fileName"]);
      pageNumber = Convert.ToInt32(context.Request.QueryString["page"]);

      // convert the image
      convertor = new Pdf2Image(fileName);
      image = convertor.GetImage(pageNumber);

      // set the content type
      context.Response.ContentType = "image/png";

      // save the image directly to the response stream
      image.Save(context.Response.OutputStream, ImageFormat.Png);
    }

    public bool IsReusable
    { get { return true; } }
  }
}



恩,这也是一段很简单的代码。先通过查询字符串来提取pdf的文件名以及页码索引,然后创建一个Pdf2Image的对象,从pdf文件的指定页中获取图像。 接下来,你需要设置Response的 ContentType对象来让浏览器知道该如何处理。最后我们将图片的流对象直接存储到 Response的 OutputStream属性中。


简单的导航

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

using System;
using System.Collections.Specialized;
using System.Web;
using Cyotek.GhostScript.PdfConversion;

namespace GhostScriptWebTest
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void previousLinkButton_Click(object sender, EventArgs e)
    {
      this.IncrementPage(-1);
    }

    protected void nextLinkButton_Click(object sender, EventArgs e)
    {
      this.IncrementPage(1);
    }

    private void IncrementPage(int increment)
    {
      NameValueCollection queryString;
      int pageNumber;
      string pdfFileName;
      Pdf2Image converter;

      queryString = HttpUtility.ParseQueryString(pdfImage.ImageUrl.Substring(pdfImage.ImageUrl.IndexOf("?")));
      pdfFileName = queryString["fileName"];
      pageNumber = Convert.ToInt32(queryString["page"]) + increment;
      converter = new Pdf2Image(this.Server.MapPath("~/" + pdfFileName));

      if (pageNumber > 0 && pageNumber <= converter.PageCount)
        pdfImage.ImageUrl = string.Format("~/PdfImage.ashx?fileName={0}&page={1}", pdfFileName, pageNumber);
    }
  }
}


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

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。 2KB翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。


2KB项目(www.2kb.com,源码交易平台),提供担保交易、源码交易、虚拟商品、在家创业、在线创业、任务交易、网站设计、软件设计、网络兼职、站长交易、域名交易、链接买卖、网站交易、广告买卖、站长培训、建站美工等服务

  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【计算机/互联网|】Nginx出现502错误(2020-01-20 21:02)
【计算机/互联网|】网站运营全智能软手V0.1版发布(2020-01-20 12:16)
【计算机/互联网|】淘宝这是怎么了?(2020-01-19 19:15)
【行业动态|】谷歌关闭小米智能摄像头,因为窃听器显示了陌生人家中的照片(2020-01-15 09:42)
【行业动态|】据报道谷歌新闻终止了数字杂志,退还主动订阅(2020-01-15 09:39)
【行业动态|】康佳将OLED电视带到美国与LG和索尼竞争(2020-01-15 09:38)
【行业动态|】2020年最佳AV接收机(2020-01-15 09:35)
【行业动态|】2020年最佳流媒体设备:Roku,Apple TV,Firebar,Chromecast等(2020-01-15 09:31)
【行业动态|】CES 2020预览:更多的流媒体服务和订阅即将到来(2020-01-08 21:41)
【行业动态|】从埃隆·马斯克到杰夫·贝佐斯,这30位人物定义了2010年代(2020-01-01 15:14)
联系我们

Q Q: 7090832

电话:400-0011-990

邮箱:7090832@qq.com

时间:9:00-23:00

联系客服
商家入住 服务咨询 投拆建议 联系客服
0577-67068160
手机版

扫一扫进手机版
返回顶部