c# - 如何在 Windows 中使用 UniDrv 或 XPS 驱动程序 v3 设置虚拟打印机和显示器

标签 c# windows xps wdk virtual-printer

我正在尝试创建一个虚拟打印机,以便在我自己的应用程序中捕获来自应用程序的打印输出。

我已经成功实现了一个使用 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 方法时,我收到一个异常指示 “系统找不到指定的文件”

它没有说明丢失了什么文件。我假设可能缺少一些依赖项,或者我发现的 sample.GPD 文件不好。

任何帮助将不胜感激。

最佳答案

在执行 AddPrinterDriver 之前,安装驱动程序路径中的所有文件(unidrv.dll、unidrvui.dll、sample.gpd),即 spool/drivers/x86Orx64 路径。

关于c# - 如何在 Windows 中使用 UniDrv 或 XPS 驱动程序 v3 设置虚拟打印机和显示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32339318/

相关文章:

c# - c# 中没有网格线的 epplus 图表(它是一个网络应用程序)

c# - 如何在未安装驱动程序时禁止 FTDI .NET DLL 提醒用户

java - windows下如何获取具体文件详细信息?

itextsharp - 如何使用 iText 库将 XPS 转换为 PDF?

c# - XPS转PDF有什么开源工具吗?

java - 在 Java 中将 XPS 转换为 Word

c# - GetWebResponse 在 Windows 7 上非常慢

c# - 如何使用显式成员映射为多态性配置 AutoMapper?

c# - 如何使用 C# 检查邮件已读或未读属性 (Lotus Notes)

python - 如果存在 .git 文件夹,则 pip install editable 在 Windows 中失败