.NET中的Ghost脚本pdf缩略图 [英] Ghost Script pdf thumbnail in .NET

查看:149
本文介绍了.NET中的Ghost脚本pdf缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站(ASP.NET)上显示上载的pdf文件的缩略图. 到目前为止,我已经完成了以下工作.

  1. 通过此链接,我有了使用ghostscript
  2. 然后生成pdf缩略图(开源/免费)告诉我去寻找那个包装纸

Matthew Ephraim发布了Ghostscript的开源包装程序,听起来像它可以满足您的要求并且在C#中. 链接到源代码: https://github.com/mephraim/ghostscriptsharp 链接到博客发布: http: //www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ 您可以简单地调用GeneratePageThumb方法来生成缩略图(或使用带有起始和结束页码的GeneratePageThumbs来为多个单独的页面生成缩略图,每个页面都是单独的输出文件),默认文件格式为jpeg,但是您可以可以通过使用替代的GenerateOutput方法调用来更改它以及许多其他选项,并指定诸如文件格式,页面大小等选项.

现在,按照 http ://mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/我已在Windows 8 64位系统上安装了ghostscript.

现在,我已经创建了一个包含上述项目的测试项目的解决方案,在我自己的项目中,我正在调用其项目的功能

try
        {
            GhostscriptSharpTests.GhostscriptSharpTests ss = new GhostscriptSharpTests.GhostscriptSharpTests();
            ss.GenerateSinglePageThumbnail();
        }
        catch (Exception ex)
        { 

        }

但是我遇到了一个例外:

无法加载DLL'gsdll32.dll':找不到指定的模块. (来自HRESULT的异常:0x8007007E)

解决方案

关于错误:

您遇到的错误可能是由于找不到gsdll32.dll或您使用的Ghostscript版本安装错误.对于64位系统,您需要安装具有gsdll64.dll的64位Ghostscript库.如果您为AnyCPU platfrom目标编译应用程序,则在64位系统上它将作为64位进程运行,并且您将需要gsdll64.dll.如果将应用程序编译为x86并在64位系统上运行,则您的应用程序将以32位进程运行,并且您可以使用gsdll32.dll.当您使用DllImport时,请确保要尝试调用的dll位于应用程序执行的同一(bin)文件夹中,或者可以位于Windows \ system中.如果要自定义dll位置,则可以在DllImport([DllImport("C:\Program Files\gs\gs9.14\bin\gsdll32.dll", EntryPoint = "gsapi_new_instance")])中使用完整路径,通常不建议这样做.

为什么不简单使用 Ghostscript.NET 库.这是一个经过充分测试的本地Ghostscript库包装程序,它可以让您执行所需的操作,并且与x86和x64 Ghostscript库兼容.

此处显示了如何将pdf光栅化为图像的示例代码:

I want to display thumbnails of the uploaded pdf files on my website(ASP.NET). So far I have done following things.

  1. From this link i got the idea to use ghostscript How to generate thumbnail for some pages of a PDF file?

You could probably use one of the general-purpose PDF libraries: • Ghostscript - C, available under the GPL • Poppler - C++, available under the GPL • Adobe PDF Library SDK - expensive Google reveals quite a few PDF-to-image converters which you may be able to incorporate if one of the above options doesn't work.

  1. Then Generate a pdf thumbnail (open source/free) told me to go look for that mentioned wrapper

Matthew Ephraim released an open source wrapper for Ghostscript that sounds like it does what you want and is in C#. Link to Source Code: https://github.com/mephraim/ghostscriptsharp Link to Blog Posting: http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ You can make a simple call to the GeneratePageThumb method to generate a thumbnail (or use GeneratePageThumbs with a start and end page number to generate thumbnails for multiple seperate pages, with each page being a seperate output file), default file format is jpeg but you can change it, and many other options, by using the alternate GenerateOutput method call and specify options such as file format, page size, etc...

Now while following instructions of http://mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ I have installed ghostscript on my system which is windows 8 64-bit.

Now I've created a solution containing the test project by above guy and in my own project i am calling a function of his project

try
        {
            GhostscriptSharpTests.GhostscriptSharpTests ss = new GhostscriptSharpTests.GhostscriptSharpTests();
            ss.GenerateSinglePageThumbnail();
        }
        catch (Exception ex)
        { 

        }

but I am getting an exception :

Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

解决方案

About the error:

The error you are getting could be due gsdll32.dll could not be found or a wrong Ghostscript version installation you used. For a 64bit system you need to install 64bit Ghostscript library which has gsdll64.dll. If you compile your application for AnyCPU platfrom target, on a 64bit system it will run as 64bit process and you will need gsdll64.dll. If you compile your application as x86 and run it on a 64bit system, your application will run as 32bit process and you can use gsdll32.dll. When you use DllImport make sure that dll your are trying to call is in a same (bin) folder your applicatione executes or it can be in windows\system. If you want custom dll location, you can use full path in a DllImport ([DllImport("C:\Program Files\gs\gs9.14\bin\gsdll32.dll", EntryPoint = "gsapi_new_instance")]) which is normally not recommended.

Why dont you simply use Ghostscript.NET library. It's a well tested native Ghostscript library wrapper which will allow you to do what you need and it's compatible with both x86 and x64 Ghostscript libraries.

Sample code that shows you how to rasterize pdf to image is here: https://ghostscriptnet.codeplex.com/SourceControl/latest#Ghostscript.NET/Ghostscript.NET.Samples/Samples/RasterizerSample.cs

Try different (lower) values with "desired_x_dpi" and "desired_y_dpi" and output image will be smaller.

这篇关于.NET中的Ghost脚本pdf缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆