2017-04-12 69 views
0

IISでホストされているアプリケーションを通じてネットワークプリンタにpdf(ストリームから生成)を印刷するシナリオがあります。私はPrintDocument.Print()を試して、私が直面している問題は次のとおりです。1.ドキュメントがサイズ0バイトの印刷ジョブキューにキューイングされています。 2.ドキュメントは、所有者名がmachine_nameの印刷ジョブキューにキューイングされます。 ここでiはとSystem.Drawing.Printing.PrintDocument(ByteArrayのからのPrintDocumentを生成する)PdfiumViewerを使用しようとしたコードである:両方の問題についてIISでホストされているアプリケーションを通じてネットワークプリンタに印刷する方法

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(); 
       } 
      } 

答えて

0

、答えは、印刷を行うユーザを偽装されています。

私の場合、アプリケーションプールはLocalSystemアカウントで動作していますが、明らかにドメインユーザーではなく、プリンタはドメインユーザーのみに公開されています。

注:アプリケーションプールを使用すると、32ビットを使用する場合はここにも記述されている課題の別のセットに直面するだろう、64ビットである:

:以下 https://blogs.msdn.microsoft.com/winsdk/2015/05/19/printing-successfully-using-impersonation-from-a-32-bit-application-on-a-64-bit-system/

をドメインユーザーの偽装を行うために必要なコードです

[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; } 
} 
+0

これは、あなたがこれに頼ることができるようにテストされています。 –

0

もう1つの可能な解決方法は、ネットワークプリンタで印刷するアクセス権またはアクセス許可を持つカスタム/ドメインユーザーにアプリケーションプールIDを構成することです。

+0

ありがとうございますので、これに頼ることができます。ありがとうございました –

関連する問題