我正在尝试创建虚拟打印机以从我自己的应用程序中的应用程序中捕获打印输出。
我已经成功实现了一个使用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;
}
}
安装Printer方法(没有正确的验证和记录):
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文件不好。
非常感谢任何帮助。
答案 0 :(得分:1)
在执行AddPrinterDriver之前,在驱动程序路径中安装所有文件(unidrv.dll,unidrvui.dll,sample.gpd),即。,spool / drivers / x86Orx64路径。