代码之家  ›  专栏  ›  技术社区  ›  Prabhu

打印自ASP.NET到网络打印机

  •  10
  • Prabhu  · 技术社区  · 16 年前

    我需要将文档发送到网络打印机(\myserver\myprinter)。我用的是系统打印类来打印,当它来自Windows服务时,它可以正常工作,但是来自ASP.NET应用程序,它只能打印到本地打印机,不能打印到网络打印机。我得到的错误是“打印机名称无效”这是我用来获取打印机名称的:

    public string PrinterName
    {
       using (LocalPrintServer server = new LocalPrintServer())
       return server.GetPrintQueue(@"\\myserver\myprinter");
    }
    

    3 回复  |  直到 16 年前
        1
  •  5
  •   Kevin LaBranche    16 年前

    您可以通过模拟或提升运行web应用的用户的权限来解决凭据问题。

    但是,我们通过在服务器上添加网络打印机作为打印机(在服务器上添加打印机对话框)并将作业发送到该打印机来实现。

    Public Class SpecialReportPrintJob
      Inherits Printing.PrintDocument
    
    Protected Overrides Sub OnBeginPrint(ByVal ev as Printing.PrintEventArgs)
      MyBase.OnBeginPrint(ev)
    
      Me.PrinterSettings.PrinterName = "PrinterNameUsedOnServer"
    
      'setup rest of stuff....
    End Sub  
    End Class
    'And we then call it like so
    Dim printSpecialReport as new SpecialReportPrintJob()
    printSpecialReport.Print()
    
        2
  •  5
  •   Henk Holterman    16 年前

    相比之下,Windows服务通常在本地系统帐户下运行(具有高权限)

        3
  •  1
  •   kiran cornelio    10 年前

    网络印刷ASP.Net/C#可以使用以下方法完成:

    如果为域用户配置了网络并将打印机添加到打印服务器:

    • PrinterName定义为“\\PrintServerIP\u或\u Name\\PrinterName” 例子:打印机设置.PrinterName=“\\15.1.1.1\\prn001”
    • 不是域用户就是所有人
    • 如果是域用户,则C代码可以包含在模拟中,该模拟可用于调用打印代码,如下所示:
    /// <summary>
        /// Does the actual impersonation.
        /// </summary>
        /// <param name="userName">The name of the user to act as.</param>
        /// <param name="domainName">The domain name of the user to act as.</param>
        /// <param name="password">The password of the user to act as.</param>
        private void ImpersonateValidUser(
            string userName, 
            string domain, 
            string password )
        {
            WindowsIdentity tempWindowsIdentity = null;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;
    
            try
            {
                if ( RevertToSelf() )
                {
                    if ( LogonUser(
                        userName, 
                        domain, 
                        password, 
                        LOGON32_LOGON_INTERACTIVE,
                        LOGON32_PROVIDER_DEFAULT, 
                        ref token ) != 0 )
                    {
                        if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 )
                        {
                            tempWindowsIdentity = new WindowsIdentity( tokenDuplicate );
                            impersonationContext = tempWindowsIdentity.Impersonate();
                        }
                        else
                        {
                            throw new Win32Exception( Marshal.GetLastWin32Error() );
                        }
                    }
                    else
                    {
                        throw new Win32Exception( Marshal.GetLastWin32Error() );
                    }
                }
                else
                {
                    throw new Win32Exception( Marshal.GetLastWin32Error() );
                }
            }
            finally
            {
                if ( token!= IntPtr.Zero )
                {
                    CloseHandle( token );
                }
                if ( tokenDuplicate!=IntPtr.Zero )
                {
                    CloseHandle( tokenDuplicate );
                }
            }
        }
    
        /// <summary>
        /// Reverts the impersonation.
        /// </summary>
        private void UndoImpersonation()
        {
            if ( impersonationContext!=null )
            {
                impersonationContext.Undo();
            }   
        }
    
        private WindowsImpersonationContext impersonationContext = null;
    

    首先调用模拟用户,然后调用如下所示的print函数:

    if(ImpersonateValidUser("username", "domain", "password"))
    {
      PrintDetails();
      UndoImpersonation();
    }
    推荐文章