Ghostscript Dll的简单VB.Net包装器

时间:2013-06-04 23:58:51

标签: vb.net dll ghostscript

我爱Ghostscript。您可以使用它将pdf转换为图形文件,拆分和/或合并pdf文件,制作缩略图以及一大堆其他内容。而且,它是免费的开源软件!

网站上有大量帖子关于如何从命令行使用Ghostscript用于各种平台。但是,我永远找不到使用Ghostscript dll(gsdll32.dll)的简单 vb.net dll包装器,而不是启动一个进程来运行Ghostscript命令行应用程序。

所以,我提出了这个代码。我在这里发帖,希望其他人可以避免我想要寻找简单直接的挫折感。它避免了你在某些代码中看到的那些愚蠢的字节数组对象数组。它具有最小的错误处理,但可以添加以适合您的应用程序。

将此代码放在名为“GhostscriptDllLib”的模块中。

Option Explicit On
Imports System.Runtime.InteropServices

'--- Simple VB.Net wrapper for Ghostscript gsdll32.dll

'    (Tested using Visual Studio 2010 and Ghostscript 9.06)

Module GhostscriptDllLib

  Private Declare Function gsapi_new_instance Lib "gsdll32.dll" _
    (ByRef instance As IntPtr, _
    ByVal caller_handle As IntPtr) As Integer

  Private Declare Function gsapi_set_stdio Lib "gsdll32.dll" _
    (ByVal instance As IntPtr, _
    ByVal gsdll_stdin As StdIOCallBack, _
    ByVal gsdll_stdout As StdIOCallBack, _
    ByVal gsdll_stderr As StdIOCallBack) As Integer

  Private Declare Function gsapi_init_with_args Lib "gsdll32.dll" _
    (ByVal instance As IntPtr, _
    ByVal argc As Integer, _
    <MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPStr)> _
    ByVal argv() As String) As Integer

  Private Declare Function gsapi_exit Lib "gsdll32.dll" _
    (ByVal instance As IntPtr) As Integer

  Private Declare Sub gsapi_delete_instance Lib "gsdll32.dll" _
    (ByVal instance As IntPtr)

  '--- Run Ghostscript with specified arguments

  Public Function RunGS(ByVal ParamArray Args() As String) As Boolean

    Dim InstanceHndl As IntPtr
    Dim NumArgs As Integer
    Dim StdErrCallback As StdIOCallBack
    Dim StdInCallback As StdIOCallBack
    Dim StdOutCallback As StdIOCallBack

    NumArgs = Args.Count

    StdInCallback = AddressOf InOutErrCallBack
    StdOutCallback = AddressOf InOutErrCallBack
    StdErrCallback = AddressOf InOutErrCallBack

    '--- Shift arguments to begin at index 1 (Ghostscript requirement)

    ReDim Preserve Args(NumArgs)
    System.Array.Copy(Args, 0, Args, 1, NumArgs)

    '--- Start a new Ghostscript instance

    If gsapi_new_instance(InstanceHndl, 0) <> 0 Then
      Return False
      Exit Function
    End If

    '--- Set up dummy callbacks

    gsapi_set_stdio(InstanceHndl, StdInCallback, StdOutCallback, StdErrCallback)

    '--- Run Ghostscript using specified arguments

    gsapi_init_with_args(InstanceHndl, NumArgs + 1, Args)

    '--- Exit Ghostscript

    gsapi_exit(InstanceHndl)

    '--- Delete instance

    gsapi_delete_instance(InstanceHndl)

    Return True

  End Function

  '--- Delegate function for callbacks

  Private Delegate Function StdIOCallBack(ByVal handle As IntPtr, _
    ByVal Strz As IntPtr, ByVal Bytes As Integer) As Integer

  '--- Dummy callback for standard input, standard output, and errors

  Private Function InOutErrCallBack(ByVal handle As IntPtr, _
    ByVal Strz As IntPtr, ByVal Bytes As Integer) As Integer

    Return 0

  End Function

End Module

gsdll32.dll文件必须是Windows可以找到的文件,最好是“\ Windows \ System32”(或64位计算机上的“\ Windows \ SysWOW64”)或与程序集相同的文件夹。这不是必须注册的dll类型(实际上,它无法注册)。

然后可以使用像这样的参数数组运行Ghostscript(此示例将pdf文件转换为高质量的png文件):

Dim PdfFilePath As String = "<Your pdf file path>"
Dim PngFilePath As String = "<Your png file path>"

RunGS("-q", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-sDEVICE=png16m", _
  "-r600", _"-dDownScaleFactor=6", "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4", _
  "-sPAPERSIZE=letter", "-sOutputFile=" & PngFilePath, PdfFilePath)

或者您可以使用像这样的字符串数组运行代码(如果在运行时动态生成参数,则更好):

Dim PdfFilePath As String = "<Your pdf file path>"
Dim PngFilePath As String = "<Your png file path>"

Dim Args() As String = {"-q", "-dNOPAUSE", "-dBATCH", "-dSAFER", _
  "-sDEVICE=png16m", "-r600", "-dDownScaleFactor=6", "-dTextAlphaBits=4", _
  "-dGraphicsAlphaBits=4", "-sPAPERSIZE=letter", _
  "-sOutputFile=" & PngFilePath, PdfFilePath}

RunGS(Args)

注意:

  • 请勿将输入或输出文件名(路径)括在引号中 将用于命令行应用程序
  • 不要转义反斜杠(例如“c:path \ file.pdf”没问题,“c:path \\ file.pdf”不是)
  • 不要使用Ghostscript“-o”开关;使用“sOutputFile =”

0 个答案:

没有答案