Prosty VB.Net Wrapper dla Ghostscript Dll [zamknięty]
Kocham Ghostscript. Możesz go używać do konwertowania plików PDF na pliki graficzne, dzielenia i / lub łączenia plików PDF, tworzenia miniatur i całej gamy innych rzeczy. I to jest darmowe oprogramowanie open-source!
Istnieją skrypty postów na stronach internetowych na temat korzystania z Ghostscript z linii poleceń dla wszystkich rodzajów platform. Ale nigdy nie mogłem znaleźćprosty vb.net dll wrapper, który używał biblioteki Ghostscript dll (gsdll32.dll) zamiast uruchamiać proces uruchamiania aplikacji wiersza poleceń Ghostscript.
Więc wpadłem na ten kod. Zamieszczam to tutaj w nadziei, że inni mogą uniknąć frustracji, której szukałem, szukając czegoś prostego i prostego. Unika tych niemądrych tablic obiektów tablic bajtów widocznych w jakimś kodzie. Ma minimalną obsługę błędów, ale można ją dodać, aby dopasować ją do aplikacji.
Umieść ten kod w module o nazwie „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
Plik gsdll32.dll musi znajdować się tam, gdzie Windows może go znaleźć, najlepiej w „Windows System32” (lub „Windows SysWOW64” na komputerze 64-bitowym) lub w tym samym folderze, co zespół. To nie jest typ biblioteki DLL, który musi zostać zarejestrowany (w rzeczywistości nie można go zarejestrować).
Następnie możesz uruchomić Ghostscript przy użyciu tablicy parametrów takiej jak ta (ten przykład konwertuje plik pdf na plik png wysokiej jakości):
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)
Lub możesz uruchomić kod za pomocą tablicy łańcuchów takiej jak ta (lepiej, jeśli argumenty są generowane dynamicznie w czasie wykonywania):
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)
Uwagi:
Nie umieszczaj nazw plików wejściowych lub wyjściowych (ścieżek) w cudzysłowach, tak jak w przypadku aplikacji wiersza poleceńNie unikaj ukośników (tzn. „C: ścieżka plik.pdf” jest w porządku, „c: ścieżka plik.pdf” nie jest)Nie używaj przełącznika Ghostscript „-o”; użyj „sOutputFile =”