如何确定 Adobe PDF 打印机所使用的 "Ne" 端口?(注:此为提问标题,无需回答)

4
我该如何检测打印机所在的端口(Ne01:,Ne02:,Ne99:等)?
我们公司的计算机(WinXP)安装了Adobe Acrobat(7.0专业版),其提供了一个名为“Adobe PDF”的虚拟打印机。如果您将Excel(2003)工作簿打印为pdf并记录宏,则打印机的完整名称为“Adobe PDF on Nexx:”,其中xx是双位数....并且因尝试的计算机不同而有所不同。
我使用Excel.Interop编写了一个C#控制台应用程序,它打开一系列电子表格。它在每个电子表格中运行宏,将其保存,打印为pdf,然后将pdf移动到共享驱动器上的报告文件夹中。
我面临的问题是,Acrobat的每个安装似乎都会为PDF打印机选择一个随机端口号......我无法找到它的位置。
到目前为止,我已经尝试使用Win32_Printer类,如下所示。
var searcher = new ManagementObjectSearcher( @"SELECT * FROM Win32_Printer" );
foreach ( ManagementObject printer in searcher.Get() )
{
   if ( Regex.IsMatch( printer["Name"].ToString(), @"(adobe|pdf)", RegexOptions.IgnoreCase ) )
   {
       //printer["Name"];    => "Adobe PDF"
       //printer["PortName"] => "my documents/*.pdf"
       foreach ( PropertyData pd in printer.Properties )
       {
           Console.WriteLine(string.Format("{0}, {1}", pd.Name, pd.Value));
       }
           break;
      }
}

我还查看了System.Drawing.Printing类。PrinterSettings.InstalledPrinters将给出打印机名称“Adobe PDF”,但我无法弄清如何获取端口信息。
如果我只传递“Adobe PDF”到Excel interop PrintOut()方法,有时它会起作用,有时会失败并显示“文档打印失败”...我无法弄清原因。
如果我传递一个硬编码的“Adobe PDF on Ne0x:”和一个适当的x值,它每次都能工作。
如果我尝试每种可能的变化,Excel会自动打印到默认打印机。我没有更改默认打印机的选项(安全策略限制)。
有人能指向正确提取打印机端口的代码吗?
3个回答

5
这是我最终采取的做法。
using Microsoft.Win32;
...

        var devices = Registry.CurrentUser.OpenSubKey( @"Software\Microsoft\Windows NT\CurrentVersion\Devices" ); //Read-accessible even when using a locked-down account
        string printerName = "Adobe PDF";

        try
        {

            foreach ( string name in devices.GetValueNames() )
            {
                if ( Regex.IsMatch( name, printerName, RegexOptions.IgnoreCase ) )
                {
                    var value = (String)devices.GetValue( name );
                    var port = Regex.Match( value, @"(Ne\d+:)", RegexOptions.IgnoreCase ).Value;  
                    return printerName + " on " + port;
                }
            }
        }
        catch
        {
            throw;
        }

1
上次我使用Acrobat时,它总是安装在LPT1上,从而避免了问题。但我认为你必须在注册表中搜索,HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices里面有它们。

0

正如你所发现的,你必须查询注册表,这是我使用的方法[在VBA中],我从Chip Pearson的优秀Excel网站上得到了这个方法:

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' modListPrinters
' By Chip Pearson, chip@cpearson.com  www.cpearson.com
' Created 22-Sept-2012
' This provides a function named GetPrinterFullNames that
' returns a String array, each element of which is the name
' of a printer installed on the machine.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const HKCU = HKEY_CURRENT_USER
Private Const KEY_QUERY_VALUE = &H1&
Private Const ERROR_NO_MORE_ITEMS = 259&
Private Const ERROR_MORE_DATA = 234
Private Const REG_SZ = 1

Private Declare Function RegOpenKeyEx Lib "advapi32" _
    Alias "RegOpenKeyExA" ( _
    ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) As Long

Private Declare Function RegEnumValue Lib "ADVAPI32.DLL" _
    Alias "RegEnumValueA" ( _
    ByVal hKey As Long, _
    ByVal dwIndex As Long, _
    ByVal lpValueName As String, _
    lpcbValueName As Long, _
    ByVal lpReserved As Long, _
    lpType As Long, _
    lpData As Byte, _
    lpcbData As Long) As Long

Private Declare Function RegCloseKey Lib "ADVAPI32.DLL" ( _
    ByVal hKey As Long) As Long

Public Function GetPrinterFullNames() As String()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetPrinterFullNames
' By Chip Pearson, chip@cpearson.com, www.cpearson.com
' Returns an array of printer names, where each printer name
' is the device name followed by the port name. The value can
' be used to assign a printer to the ActivePrinter property of
' the Application object. Note that setting the ActivePrinter
' changes the default printer for Excel but does not change
' the Windows default printer.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim Printers() As String ' array of names to be returned
Dim PNdx As Long    ' index into Printers()
Dim hKey As Long    ' registry key handle
Dim Res As Long     ' result of API calls
Dim Ndx As Long     ' index for RegEnumValue
Dim ValueName As String ' name of each value in the printer key
Dim ValueNameLen As Long    ' length of ValueName
Dim DataType As Long        ' registry value data type
Dim ValueValue() As Byte    ' byte array of registry value value
Dim ValueValueS As String   ' ValueValue converted to String
Dim CommaPos As Long        ' position of comma character in ValueValue
Dim ColonPos As Long        ' position of colon character in ValueValue
Dim M As Long               ' string index

' registry key in HCKU listing printers
Const PRINTER_KEY = "Software\Microsoft\Windows NT\CurrentVersion\Devices"

PNdx = 0
Ndx = 0
' assume printer name is less than 256 characters
ValueName = String$(256, Chr(0))
ValueNameLen = 255
' assume the port name is less than 1000 characters
ReDim ValueValue(0 To 999)
' assume there are less than 1000 printers installed
ReDim Printers(1 To 1000)

' open the key whose values enumerate installed printers
Res = RegOpenKeyEx(HKCU, PRINTER_KEY, 0&, _
    KEY_QUERY_VALUE, hKey)
' start enumeration loop of printers
Res = RegEnumValue(hKey, Ndx, ValueName, _
    ValueNameLen, 0&, DataType, ValueValue(0), 1000)
' loop until all values have been enumerated
Do Until Res = ERROR_NO_MORE_ITEMS
    M = InStr(1, ValueName, Chr(0))
    If M > 1 Then
        ' clean up the ValueName
        ValueName = Left(ValueName, M - 1)
    End If
    ' find position of a comma and colon in the port name
    CommaPos = InStr(1, ValueValue, ",")
    ColonPos = InStr(1, ValueValue, ":")
    ' ValueValue byte array to ValueValueS string
    On Error Resume Next
    ValueValueS = Mid(ValueValue, CommaPos + 1, ColonPos - CommaPos)
    On Error GoTo 0
    ' next slot in Printers
    PNdx = PNdx + 1
    Printers(PNdx) = ValueName & " on " & ValueValueS
    ' reset some variables
    ValueName = String(255, Chr(0))
    ValueNameLen = 255
    ReDim ValueValue(0 To 999)
    ValueValueS = vbNullString
    ' tell RegEnumValue to get the next registry value
    Ndx = Ndx + 1
    ' get the next printer
    Res = RegEnumValue(hKey, Ndx, ValueName, ValueNameLen, _
        0&, DataType, ValueValue(0), 1000)
    ' test for error
    If (Res <> 0) And (Res <> ERROR_MORE_DATA) Then
        Exit Do
    End If
Loop

' shrink Printers down to used size
ReDim Preserve Printers(1 To PNdx)
Res = RegCloseKey(hKey)
' Return the result array
GetPrinterFullNames = Printers
End Function

我会使用这个函数来获取PDF打印机的名称:
Public Function FindPDFPrinter() As String
'this function finds the exact printer name for the Adobe PDF printer

        Dim Printers() As String
        Dim N As Integer
        FindPDFPrinter = ""
        Printers = GetPrinterFullNames()
        For N = LBound(Printers) To UBound(Printers)
            If InStr(1, Printers(N), "PDF") Then
              FindPDFPrinter = Printers(N)
            End If
        Next N

End Function

然后,您将Application.ActivePrinter设置为该字符串。

如果您只需要端口,可以从字符串末尾获取它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接