我正在尝试创建一个虚拟打印机来捕获我自己应用程序中应用程序的打印输出。
我已经成功实现了一个使用 Microsoft PostScript 驱动程序并生成 ps 文件的程序。 (很多代码摘自不同的开源项目)
但是,由于生产服务器上 GhostScript 的许可问题(它不是免费的商业解决方案),我想实现一个不同的驱动程序来生成 XPS 文件或我可以用来提取文本、转换为 PDF 的任何其他格式,提取每个页面的图像等。
我与 postscript 驱动程序一起使用并且实际上运行良好的代码如下:
// Declare the AddPrinterDriver as extern.
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool AddPrinterDriver(String pName, UInt32 Level, ref DRIVER_INFO_3 pDriverInfo);
// Create a function to call it.
public void AddPrinterDriver(string driverName, string driverPath, string dataPath, string configPath, string helpPath)
{
DRIVER_INFO_3 di = new DRIVER_INFO_3();
di.cVersion = 3;
di.pName = driverName;
di.pEnvironment = null;
di.pDriverPath = driverPath;
di.pDataFile = dataPath;
di.pConfigFile = configPath;
di.pHelpFile = helpPath;
di.pDependentFiles = "";
di.pMonitorName = null;
di.pDefaultDataType = "RAW";
if (!AddPrinterDriver(null, 3, ref di))
{
Exception exc = new Win32Exception(Marshal.GetLastWin32Error());
throw exc;
}
}
安装打印机方法(没有正确的验证和记录):
public void InstallVirtualPrinter()
{
// Declare file names for PostScript printer driver. (Preinstalled in Vista and Up)
string driverFileName = "PSCRIPT5.DLL";
string configFileName = "PS5UI.DLL";
string helpFileName = "PSCRIPT.HLP";
string dataFileName = "MyCustomConfig.PPD";
string driverPath = null;
string dataPath = null;
string configPath = null;
string helpPath = null;
try
{
//Set Printer Driver Path and Files.
string printerDriverPath = Path.Combine(GetPrinterDirectory(), "3");
// Set the path for the driver files.
if (!String.IsNullOrWhiteSpace(printerDriverPath))
{
driverPath = string.Format("{0}\\{1}", printerDriverPath, driverFileName);
dataPath = string.Format("{0}\\{1}", printerDriverPath, dataFileName);
configPath = string.Format("{0}\\{1}", printerDriverPath, configFileName);
helpPath = string.Format("{0}\\{1}", printerDriverPath, helpFileName);
}
// Add Printer Monitor
if (!DoesMonitorExist(PrinterMonitorName))
{
AddPrinterMonitor(PrinterMonitorName);
}
// Add Printer Port
if (!DoesPrinterPortExist(PrinterPortName))
{
AddPrinterPort(PrinterPortName, PrinterMonitorName);
}
// Add Printer Driver
if (!DoesPrinterDriverExist(PrinterDriverName))
{
//
//
//
//
// This references the above method in this SO question.
AddPrinterDriver(PrinterDriverName, driverPath, dataPath, configPath, helpPath);
//
// This fails when trying with a driver different than PScript.
//
}
// Add Printer
if (!DoesPrinterExist(PrinterName))
{
InstallPrinter(PrinterName, PrinterPortName, PrinterDriverName);
}
// Configure Virtual Port
ConfigureVirtualPort(PrinterMonitorName, PrinterPortName);
// Restart Spool Service
RestartSpoolService();
log.Info("Virtual printer installation completed successfully");
return;
}
catch (Exception exc)
{
log.ErrorFormat("An exception has been raise when attempting to install the printer \n{0}", exc);
}
}
所以这是一个问题:
如何使用不同的驱动程序,如 UniDrv 或 XPS 来实现虚拟打印机/监视器?.
我通过替换上面代码中的以下行来尝试使用 UniDrv:
string driverFileName = "unidrv.dll";
string dataFileName = "sample.GPD";
string configFileName = "unidrvui.dll";
string helpFileName = "unidrv.hlp";
当我运行该方法时,AddPrinterDriver
我得到一个异常指示"The system cannot find the file specified"
。
它没有说缺少什么文件。我假设可能缺少一些依赖项,或者我发现的 sample.GPD 文件不好。
任何帮助将不胜感激。