是否可以使用itextSharp将PDF页面转换为图像?

Max*_*Max 8 c# pdf asp.net itextsharp

您好我一直在使用itextSharp为dot.net中的所有pdf相关项目.我遇到了一个需要将PDF页面转换为图像的要求.我找不到任何这样的样品.我发现另一个工具ghostscript能够做到这个问题是我在共享主机上我不认为ghostscript将在服务器上运行,因为在我的本地机器我不得不手动将ghost脚本dll复制到system32文件夹在共享主机中是不可能的.

Max*_*Max 13

好的,我搜索了一遍,发现Ghost脚本有一个nuget包,所以通过打包管理器控制台并将ghost脚本添加到新项目来解决我的问题(我创建了一个新项目,因为旧的项目有各种各样的通过"PM> Install-Package Ghostscript.NET"引用win32 ghostscript dlls).所以我的问题的答案是:1.> itextSharp 无法直接将PDF页面转换为图像.2.>"Ghostscript.NET 1.2.0"非常容易实现.以下是一个代码示例.

    public void LoadImage(string InputPDFFile,int PageNumber)
    {

        string outImageName = Path.GetFileNameWithoutExtension(InputPDFFile);
        outImageName = outImageName+"_"+PageNumber.ToString() + "_.png";


        GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png256);
        dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
        dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
        dev.ResolutionXY = new GhostscriptImageDeviceResolution(290, 290);
        dev.InputFiles.Add(InputPDFFile);
        dev.Pdf.FirstPage = PageNumber;
        dev.Pdf.LastPage = PageNumber;
        dev.CustomSwitches.Add("-dDOINTERPOLATE");
        dev.OutputPath = Server.MapPath(@"~/tempImages/" + outImageName);
        dev.Process();

    }
Run Code Online (Sandbox Code Playgroud)