我正在使用 Ghostscipt 9.54(最新版本)将 pdf 文件打印到我的 Windows 打印机。我正在使用以下命令:
C:\Users\Pradeep Gupta>"C:\Program Files\gs\gs9.54.0\bin\gswin64c.exe" -dBATCH -dNOPAUSE -dSAFER -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=%printer%"Everycom-58-Series" -f C:\PDF\5601001234040921211737.pdf
GPL Ghostscript 9.54.0 (2021-03-30)
Copyright (C) 2021 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Processing pages 1 through 1.
Page 1
这工作正常并且按预期工作。
现在我尝试在我的 .NET C# 应用程序中复制同样的东西。但问题是,每当我在我的 Win 10 笔记本电脑上运行我的应用程序时,它都会显示打印对话框以选择打印机。请让我知道我做错了什么,因为我正在复制与上述命令中所述相同的参数。下面是我的 C# 应用程序的代码:
private static bool DoGhostscriptPrint(string printerName, string pdfFilename)
{
try
{
String[] ghostScriptArguments = { "-dBATCH", "-dNOPAUSE", "-dSAFER", "-dNoCancel",
"-dNumCopies=1", "-sDEVICE=mswinpr2", String.Format("-sOutputFile=\"%printer%{0}\"", printerName),
"-f", pdfFilename};
GhostScript.CallAPI(ghostScriptArguments);
}
catch (Exception ex)
{
Logger.Error("Unable to Print using Ghostscript: " + ex.Message + Environment.NewLine + ex.StackTrace);
}
return false;
}
我正在使用https://github.com/mephraim/ghostscriptsharp中的代码来调用带有 args 的 Ghostscript。这是 Ghosctscript.CallAPI() 的代码
/// Calls the Ghostscript API with a collection of arguments to be passed to it
/// </summary>
public static void CallAPI(string[] args)
{
// Get a pointer to an instance of the Ghostscript API and run the API with the current arguments
IntPtr gsInstancePtr;
Logger.Debug("Acquiring Lock to call GS API with Args {0}", String.Join(",", args));
lock (resourceLock)
{
Logger.Debug("Lock Acquired");
GhostScriptNativeWrapper.CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
try
{
int result = GhostScriptNativeWrapper.InitAPI(gsInstancePtr, args.Length, args);
if (result < 0)
{
throw new ExternalException("Ghostscript conversion error", result);
}
}
finally
{
Logger.Debug("Lock Released");
Cleanup(gsInstancePtr);
Logger.Debug("GS Cleanup Done");
}
}
}
InitAPI 代码为:
[DllImport("gsdll64.dll", EntryPoint = "gsapi_init_with_args")]
internal static extern int InitAPI(IntPtr instance, int argc, string[] argv);
我已经从 Ghoscript 安装中复制了 gsdll64.dll。 从上周开始我就面临这个问题。自一个月以来,应用程序代码没有变化。
谢谢和问候, Pradeep Gupta