如何在Windows中使用UniDrv或XPS驱动程序v3设置虚拟打印机和监视器
本文关键字:设置 v3 驱动程序 虚拟 打印机 监视器 XPS Windows UniDrv | 更新日期: 2023-09-27 18:10:48
我正在尝试创建一个虚拟打印机来捕获我自己的应用程序中的应用程序的打印输出。
我已经成功地实现了一个使用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"
。
它没有说明丢失了什么文件。我假设可能缺少一些依赖项或样本。我发现GPD文件不好。
执行AddPrinterDriver前,请先安装驱动路径ie下的所有文件(unidrvi .dll、unidrvi .dll、sample.gpd)。, spool/drivers/x86Orx64 path.