Simple VB.Net Wrapper für Ghostscript Dll [geschlossen]
Ich liebe Ghostscript. Sie können damit PDFs in Grafikdateien konvertieren, PDF-Dateien teilen und / oder zusammenführen, Miniaturansichten erstellen und eine ganze Reihe anderer Dinge. Und es ist kostenlose Open-Source-Software!
Auf Websites gibt es unzählige Beiträge zur Verwendung von Ghostscript über die Befehlszeile für alle Arten von Plattformen. Aber ich konnte nie einen findeneinfach vb.net-DLL-Wrapper, der die Ghostscript-DLL (gsdll32.dll) verwendete, anstatt einen Prozess zum Ausführen der Ghostscript-Befehlszeilen-App zu starten.
Also habe ich mir diesen Code ausgedacht. Ich poste es hier in der Hoffnung, dass andere die Frustration vermeiden können, die ich nach etwas Einfachem und Unkompliziertem suchte. Sie vermeidet die doofen Arrays von Byte-Array-Objekten, die Sie in irgendeinem Code sehen. Es verfügt über eine minimale Fehlerbehandlung, die jedoch entsprechend Ihrer Anwendung hinzugefügt werden kann.
Fügen Sie diesen Code in ein Modul mit dem Namen "GhostscriptDllLib" ein.
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
Die Datei gsdll32.dll muss sich dort befinden, wo Windows sie finden kann, am besten unter "\ Windows \ System32" (oder "\ Windows \ SysWOW64" auf einem 64-Bit-Computer) oder im selben Ordner wie Ihre Assembly. Es ist nicht die Art von DLL, die registriert werden muss (in der Tat kann es nicht registriert werden).
Sie können Ghostscript dann mit einem Parameterarray wie diesem ausführen (in diesem Beispiel wird eine PDF-Datei in eine qualitativ hochwertige PNG-Datei konvertiert):
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)
Oder Sie können den Code mit einem String-Array wie diesem ausführen (besser, wenn Argumente zur Laufzeit dynamisch generiert werden):
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)
Anmerkungen:
Schließen Sie keine Eingabe- oder Ausgabedateinamen (Pfade) in Anführungszeichen ein, wie dies für die Befehlszeilenanwendung der Fall wäreVermeiden Sie keine umgekehrten Schrägstriche (d. H. "C: path \ file.pdf" ist in Ordnung, "c: path \\ file.pdf" nicht).Verwenden Sie nicht den Ghostscript-Schalter "-o". benutze "sOutputFile ="