我正在使用Ghostscrip VB.NET包装器来打印PDF文件,而无需在用户的计算机上安装GhostScript exe,也不需要安装Adobe。
它非常好用。但问题是,我需要打印许多文件,每次发送一些文件进行打印时,我都无法停止默认的打印机对话框。
我不介意打印机对话框出现,因为我希望用户可以选择打印机进行打印,但我无法弄清楚如何在一个命令中将多个文件发送到打印。
目的是为许多文件显示一次打印机对话框。这是我的代码:
RunGS(“ - q”,“ - dNOPAUSE”,“ - dNoCancel”,“ - dBATCH”,“ - dSAFER”,“ - sDEVICE = mswinpr2”,路径)
RunGS - 是包装函数 路径随文件路径变化。
答案 0 :(得分:1)
Ghostscript命令行可以接受多个输入文件。如果您修改RunGS以获取可选数量的Path参数并将它们传递给基础gswin32c.exe命令行,它应该可以正常工作。
我使用下面的命令对其进行了测试,该命令打印了2份文件:
gswin32c -dBATCH -dNOPAUSE -sDEVICE=mswinpr2 file.pdf file.pdf
我不确定RunGS是什么,但是我搜索并在此处找到了以前的帖子,其中有人共享了一个直接与DLL交互的函数。如果它具有相同的功能,它似乎只是将所有的args直接传递给Ghostscript。尝试添加多个输入文件,如果是这样的话,它应该可以正常工作。
适用于我的示例:
GhostscriptDllLib.RunGS("-q", "-dNOPAUSE", "-dNoCancel", "-dBATCH", "-dSAFER", "-sDEVICE=mswinpr2", "C:\testing\Test1.pdf", "C:\testing\Test2.pdf")
答案 1 :(得分:0)
感谢您的帮助。我试过了,但没有奏效。我使用直接与dll通信的包装器。这是包装器的模块:
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
然后我尝试运行函数GS,其中参数用逗号分隔。
RunGS("-q", "-dNOPAUSE", "-dNoCancel", "-dBATCH", "-dSAFER", "-sDEVICE=mswinpr2", Path)
如果我用逗号分隔路径,则没有任何反应。如果我把路径放在一起,用空格分开。不会调用打印。
答案 2 :(得分:0)
此网站中的其他帖子为Simple VB.Net Wrapper for Ghostscript Dll。它对我也不起作用。
首先,源代码转到此部分的InOutErrCallBack方法:
gsapi_init_with_args(InstanceHndl, NumArgs + 1, Args)
当发生这种情况时,执行冻结。发生这种情况后,下一次对RunGS方法的调用在此部分中返回false:
If gsapi_new_instance(InstanceHndl, IntPtr.Zero) <> 0 Then
Return False
Exit Function
End If
gsapi_new_instance的返回值为-100: Ghostscript API - Return codes
gswin32.dll位于C:\ Windows \ System32和BIN项目文件夹中。
我也写了其他包装器,并发生了类似的事情:
Option Explicit On
Imports System.Runtime.InteropServices
'''
''' https://msdn.microsoft.com/en-us/library/aa719104(v=vs.71).aspx
''' http://www.codeproject.com/Articles/32274/How-To-Convert-PDF-to-Image-Using-Ghostscript-API
''' http://www.ghostscript.com/doc/current/API.htm
''' https://stackoverflow.com/questions/16929383/simple-vb-net-wrapper-for-ghostscript-dll
'''
Public Class GhostscriptDLLWrapper
#Region "GhostScript Import"
<StructLayout(LayoutKind.Sequential)> _
Public Structure GSVersion
Public product As IntPtr
Public copyright As IntPtr
Public revision As Integer
Public revisionDate As Integer
End Structure
'''
''' <summary>This function returns the revision numbers and strings of the Ghostscript interpreter library;
''' you should call it before any other interpreter library functions to make sure that the
''' correct version of the Ghostscript interpreter has been loaded.</summary>
'''
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_revision")> _
Public Shared Function GsapiRevision(ByRef pVer As GSVersion, ByVal pSize As Integer) As Integer
End Function
''' <summary>Create a new instance of Ghostscript.</summary>
''' <param name="pinstance"></param>
''' <param name="caller_handle"></param>
''' <returns>The instance passed to other GS function</returns>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_new_instance")> _
Public Shared Function GsapiNewInstance( _
ByRef instance As IntPtr, _
ByVal caller_handle As IntPtr) As Integer
End Function
''' <summary>This will make the conversion</summary>
''' <param name="instance"></param><param name="argc"></param>
''' <param name="argv"></param>
''' <returns>0 if is ok</returns>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_init_with_args")> _
Public Shared Function GsapiInitWithArgs( _
ByVal instance As IntPtr, _
ByVal argc As Integer, _
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.LPStr)> _
ByVal argv() As String) As Integer
End Function
''' <summary>Exit the interpreter</summary>
''' <param name="instance"></param>
''' <returns></returns>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_exit")> _
Public Shared Function GsapiExit(ByVal instance As IntPtr) As Integer
End Function
''' <summary>Destroy an instance of Ghostscript.</summary>
''' <param name="instance"></param>
<DllImport("gsdll32.dll", CharSet:=CharSet.Ansi, EntryPoint:="gsapi_delete_instance")> _
Public Shared Function GsapiDeleteInstance(ByVal instance As IntPtr)
End Function
#End Region
End Class
我用这种方式调用这些方法:
'https://stackoverflow.com/questions/16929383/simple-vb-net-wrapper-for-ghostscript-dll
'Shift arguments to begin at index 1 (Ghostscript requirement)
'Initialise the interpreter. This calls gs_main_init_with_args() in imainarg.c. See below for return codes.
'The arguments are the same as the "C" main function: argv[0] is ignored and the user supplied arguments are argv[1] to argv[argc-1].
Dim argsCount As Integer = args.Length
ReDim Preserve args(argsCount)
System.Array.Copy(args, 0, args, 1, argsCount)
SyncLock SyncRoot
Dim gsInstance As IntPtr
Dim result As Integer
result = GhostscriptDLLWrapper.GsapiNewInstance(gsInstance, IntPtr.Zero)
If (result = 0) Then
Try
result = GhostscriptDLLWrapper.GsapiInitWithArgs( _
gsInstance, _
args.Length, _
args _
)
If (result < 0) Then
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
If File.Exists(outputImgPath) Then
objFileStream = New FileStream(outputImgPath, FileMode.Open)
Dim length As Int32 = objFileStream.Length
Dim bytes(length) As Byte
objFileStream.Read(bytes, 0, length)
objFileStream.Close()
objMemoryStream = New MemoryStream(bytes, False)
objImages.Add(Image.FromStream(objMemoryStream))
Else
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
Catch ex As Exception
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG", ex)
End Try
End If
GhostscriptDLLWrapper.GsapiExit(gsInstance)
GhostscriptDLLWrapper.GsapiDeleteInstance(gsInstance)
End SyncLock
我的参数序列:
Dim outputImgPath As String
outputImgPath = "C:\Download\DocumentosV2\Protocolo\Pronunciamento\" + Guid.NewGuid.ToString("N") + ".png"
Dim args() As String = { _
"-dNOPAUSE", _
"-dBATCH", _
"-dSAFER", _
"-dQUIET", _
"-sDEVICE=png16m", _
String.Format("-r{0}", resolucao), _
"-dTextAlphaBits=2", _
"-dGraphicsAlphaBits=2", _
String.Format("-dFirstPage={0}", pageNumber), _
String.Format("-dLastPage={0}", pageNumber), _
String.Format("-sOutputFile={0}", outputImgPath), _
"-f", _
pdfPath _
}