2010-08-17 5 views
11

"print to file"을 사용하여 생성 한 PCL 파일이 있습니다.C#에서 pcl 파일을 인쇄하는 방법?

프로그래밍 방식으로 C#에서이 파일을 인쇄하는 가장 좋은 방법은 무엇입니까?

(당연히 PCL을 지원하는 프린터에서 제공됩니다).

나는 프롬프트에서 내가 호출하여 인쇄 할 수 있다는 사실을 알고 : 깨끗한 방식이있는 경우

copy filename.pcl //location/printername 

그래서 내가 프로그래밍 같은 일을 할도 수 (사용 사본) .. 궁금 상상 것 printDocument를 사용하여 말하십시오.

var pd = new PrintDocument 
     { 
      DocumentName = @"filename.pcl"; 
      PrinterSettings = {PrinterName = @"\\location\printername"} 
     }; 


pd.Print(); 

난 항상 인쇄 빈 페이지를 얻을 : 내가하는 PrintDocument를 사용할 때

참고.

+2

DocumentName은 인쇄 작업의 표시 이름입니다. 디스크의 파일 이름과 관련이 없습니다. – dthorpe

답변

2

This article applies to Visual Basic하지만 C#에 적용 할 수있을만큼 쉽습니다. 그렇지 않으면 문제가있는 부분을 기꺼이 도와 드리겠습니다.

프린터가 네트워크에 연결된 경우 here's a little funny example of how to talk directly to it. PCL의 바이트를 프린터로 보내는 경우에도 작동하는지 확실하지 않습니다.

+0

하하, 재미있는 예를 들어 +1, 아픈 직장에서 그것을 밖으로 시도해야합니다. – vicsz

2

우리는 DLL 가져 오기에 따라 다음 방법 사용 : 미안 해요

[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd); 

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    public static extern bool ClosePrinter(IntPtr hPrinter); 

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] RawPrinter di); 

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    public static extern bool EndDocPrinter(IntPtr hPrinter); 

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    public static extern bool StartPagePrinter(IntPtr hPrinter); 

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    public static extern bool EndPagePrinter(IntPtr hPrinter); 

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten); 
2

너무 늦게이 질문에 온하기를,하지만 난 일을 할 것입니다 몇 가지 코드가 있습니다. 그것은 원래 나에 의해 쓰여지지 않았다. 다른 프로그래머의 도움 사이트에서 코드를 받았지만 어느 코드가 기억이 나지 않습니다. 아름답게 작동합니다. 다음은 dll을 만드는 프로젝트입니다. 직접 만들려면 만들기 :/프로젝트/클래스 라이브러리 (프로젝트 형식에서 Visual C# 선택)를 선택하십시오.

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.IO; 

namespace PrintRaw 
{ 
    public class RawFilePrint 
    { 
     // Structure and API declarions: 
     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
     public class DOCINFOA 
     { 
     [MarshalAs(UnmanagedType.LPStr)] 
     public string pDocName; 
     [MarshalAs(UnmanagedType.LPStr)] 
     public string pOutputFile; 
     [MarshalAs(UnmanagedType.LPStr)] 
     public string pDataType; 
     } 
     [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd); 

     [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool ClosePrinter(IntPtr hPrinter); 

     [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di); 

     [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool EndDocPrinter(IntPtr hPrinter); 

     [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool StartPagePrinter(IntPtr hPrinter); 

     [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool EndPagePrinter(IntPtr hPrinter); 

     [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 
     public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten); 

     // SendBytesToPrinter() 
     // When the function is given a printer name and an unmanaged array 
     // of bytes, the function sends those bytes to the print queue. 
     // Returns true on success, false on failure. 
     public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) 
     { 
     Int32 dwError = 0, dwWritten = 0; 
     IntPtr hPrinter = new IntPtr(0); 
     DOCINFOA di = new DOCINFOA(); 
     bool bSuccess = false; // Assume failure unless you specifically succeed. 

     di.pDocName = "RAW Document"; 
     di.pDataType = "RAW"; 

     if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) 
     { 
      if (StartDocPrinter(hPrinter, 1, di)) 
      { 
       if (StartPagePrinter(hPrinter)) 
       { 
        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); 
        EndPagePrinter(hPrinter); 
       } 
       EndDocPrinter(hPrinter); 
      } 
      ClosePrinter(hPrinter); 
     } 
     if (!bSuccess) 
     { 
      dwError = Marshal.GetLastWin32Error(); 
     } 
     return bSuccess; 
     } 

     public static bool SendFileToPrinter(string szPrinterName, string szFileName) 
     { 
     FileStream fs = new FileStream(szFileName, FileMode.Open); 
     BinaryReader br = new BinaryReader(fs); 
     Byte[] bytes = new Byte[fs.Length]; 
     bool bSuccess = false; 
     IntPtr pUnmanagedBytes = new IntPtr(0); 
     int nLength; 

     nLength = Convert.ToInt32(fs.Length); 
     bytes = br.ReadBytes(nLength); 
     pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength); 
     Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength); 
     bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength); 
     Marshal.FreeCoTaskMem(pUnmanagedBytes); 
     return bSuccess; 
     } 

     public static bool SendStringToPrinter(string szPrinterName, string szString) 
     { 
     IntPtr pBytes; 
     Int32 dwCount; 
     dwCount = szString.Length; 
     // Assume that the printer is expecting ANSI text, and then convert 
     // the string to ANSI text. 
     pBytes = Marshal.StringToCoTaskMemAnsi(szString); 
     SendBytesToPrinter(szPrinterName, pBytes, dwCount); 
     Marshal.FreeCoTaskMem(pBytes); 
     return true; 
     } 
    } 
} 

이제이 코드를 사용하려면 결과 dll을 프로젝트에 대한 참조로 추가 한 다음 필요에 따라 함수를 호출하십시오. 여기에 제가 사용한 코드가 있습니다 :

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 

namespace PclFontTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      string szPrinterName = @"\\printserver\LaserJet 2420"; 

      StreamReader sr = new StreamReader(@"C:\Fonts\US20HP.FNT"); 
      string line = (char)27 + "*c32545D"; 
      line += sr.ReadToEnd(); 
      line += (char)27 + "*c5F"; 

      PrintRaw.RawFilePrint.SendStringToPrinter(szPrinterName, line); 


     } 
    } 
} 

이 프로그램은 파일에서 PCL 글꼴을 읽습니다. 32545의 글꼴 ID를 할당하는 코드로 글꼴을 래핑 한 다음 dll 함수 인 SendStringToPrinter를 호출합니다.

+0

파일로 인쇄하도록 설정된 레이저젯 4 드라이버로 전체 pcl 문서를 보내려고했습니다. 파일의 상태가 "오류 - 인쇄"라고 표시됩니다. – Jeremy

+1

파일로 인쇄하려고하는 것이 문제라고 생각합니다. . 위의 코드는 PCL 코드 만 포함 된 파일이 필요하며 Windows를 방해하지 않고 프린터로 직접 보내려합니다. 즉, 이미 파일에 인쇄 된 파일을 인쇄하려고 시도하는 것입니다. –

관련 문제