0

我有通过 IIS 中托管的应用程序将 pdf(从流生成)打印到网络打印机的场景。我尝试使用 PrintDocument.Print() 并且我面临的问题是: 1. 文档正在排队到大小为 0 字节的打印作业队列中。2. 文档正在排队到打印作业队列,所有者名称为 machine_name。这是我尝试使用 PdfiumViewer(从 bytearray 生成 PrintDocument)和 System.Drawing.Printing.PrintDocument 的代码:

 public void SendPdfToPrinter(byte[] byteArray, string fileName, string printerNetworkPath)
    {
        using (Stream fileStream = new MemoryStream(byteArray)) //byte array for the file content
        {

            var printerSettings = new System.Drawing.Printing.PrinterSettings
            {
                PrinterName = printerNetworkPath, //this is the printer full name. i.e. \\10.10.0.12\ABC-XEROX-01
                PrintFileName = fileName, //file name. i.e. abc.pdf
                PrintRange = System.Drawing.Printing.PrintRange.AllPages,
            };
            printerSettings.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);

            // Now print the PDF document
            using (PdfiumViewer.PdfDocument document = PdfiumViewer.PdfDocument.Load(fileStream))
            {
                using (System.Drawing.Printing.PrintDocument printDocument = document.CreatePrintDocument())
                {
                    printDocument.DocumentName = fileName;
                    printDocument.PrinterSettings = printerSettings;
                    printDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
                    printDocument.Print();
                }
            }
4

2 回答 2

0

对于这两个问题,答案是冒充用户进行打印。

在我的情况下,应用程序池在 LocalSystem 帐户下运行,这显然不是域用户,并且打印机仅向域用户公开。

注意:应用程序池是 64 位的,如果您使用 32 位,您将面临另一组挑战,此处详细描述: https ://blogs.msdn.microsoft.com/winsdk/2015/05/19/printing-successfully-using-在 64 位系统上模拟 32 位应用程序/

以下是为域用户进行模拟所需的代码:

 [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public class Impersonation : IDisposable
{
    private readonly SafeTokenHandle _handle;
    private readonly WindowsImpersonationContext _context;
    bool disposed = false;

    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_INTERACTIVE = 2;

    public Impersonation(ImpersonateUserDetails user) : this(user.Domain, user.UserName, user.Password)
    { }
    public Impersonation(string domain, string username, string password)
    {
        var ok = LogonUser(username, domain, password,
                       LOGON32_LOGON_INTERACTIVE, 0, out this._handle);
        if (!ok)
        {
            var errorCode = Marshal.GetLastWin32Error();
            throw new ApplicationException(string.Format("Could not impersonate the elevated user.  LogonUser returned error code {0}.", errorCode));
        }

        this._context = WindowsIdentity.Impersonate(this._handle.DangerousGetHandle());
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
            return;

        if (disposing)
        {
            this._context.Dispose();
            this._handle.Dispose();
        }           
        disposed = true;
    }

    ~Impersonation()
    {
        Dispose(false);
    }


    [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);

    sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        private SafeTokenHandle()
            : base(true) { }

        [DllImport("kernel32.dll")]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseHandle(IntPtr handle);

        protected override bool ReleaseHandle()
        {
            return CloseHandle(handle);
        }
    }
}

public class ImpersonateUserDetails
{
    public string UserName { get; set; }

    public string Password { get; set; }

    public string Domain { get; set; }
}
于 2017-04-20T05:10:41.033 回答
0

另一个可能且简单的解决方案是将您的应用程序池标识配置为自定义/域用户,该用户有权在网络打印机中打印。

于 2017-04-24T05:23:33.937 回答