2010-08-17 28 views
11

Tôi có một tệp PCL mà tôi đã tạo bằng cách sử dụng "in ra tệp".Làm cách nào để in tệp pcl trong C#?

Cách tốt nhất để lập trình in tệp này bằng C#.

(Tất nhiên là máy in tôi đang in hỗ trợ PCL).

Tôi biết rằng từ dấu nhắc tôi có thể in bằng cách gọi:

copy filename.pcl //location/printername 

Vì vậy, tôi sẽ tưởng tượng rằng tôi cũng có thể làm điều tương tự lập trình (sử dụng bản sao) .. Tôi tự hỏi, nếu có một cách sạch để làm điều này, nói bằng cách sử dụng PrintDocument.

Lưu ý rằng khi tôi sử dụng PrintDocument:

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


pd.Print(); 

tôi luôn luôn nhận được một trang trống được in ra.

+2

DocumentName là tên hiển thị của lệnh in. Không liên quan đến tên tệp trên đĩa. – dthorpe

Trả lời

2

This article applies to Visual Basic, nhưng thật dễ dàng để theo dõi để có thể áp dụng nó cho C#, tôi nghĩ vậy. Nếu không, tôi sẽ sẵn sàng hỗ trợ bạn với bất kỳ phần nào có vấn đề.

Nếu máy in đang ở trên mạng, here's a little funny example of how to talk directly to it. Không chắc chắn nếu nó cũng hoạt động nếu bạn chỉ cần gửi các byte của PCL đến máy in mặc dù.

+0

Haha, 1 cho ví dụ ngộ nghĩnh, Ill phải thử nó ở nơi làm việc. – vicsz

2

Chúng tôi sử dụng các phương pháp sau mỗi DLL nhập:

[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

Tôi xin lỗi đã đến quá muộn cho câu hỏi này, nhưng tôi có một số mã mà sẽ thực hiện công việc. Nó không được viết bởi tôi ban đầu. Tôi đã nhận được mã từ trang web trợ giúp của một lập trình viên khác, nhưng tôi không thể nhớ mã nào. Nó hoạt động rất đẹp. Đây là dự án, tạo ra một dll. Để tự làm điều đó, chọn Create:/Project/Class Library (chọn Visual C# dưới các kiểu dự án).

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

Bây giờ, để sử dụng mã này, thêm dll tạo thành tham chiếu đến dự án của bạn, sau đó gọi các hàm bạn cần. Dưới đây là một số mã mà tôi đã sử dụng nó, chỉ hôm nay:

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


     } 
    } 
} 

Chương trình này đọc PCL Phông chữ từ tệp. Nó kết thúc tốt đẹp phông chữ với mã để gán một id phông chữ của 32545 cho nó, sau đó gọi hàm dll SendStringToPrinter.

+0

Tôi đã cố gửi một tài liệu đầy đủ về trình điều khiển laserjet 4 đã được thiết lập để in ra tệp, Trạng thái trên tệp chỉ nói "Lỗi - In" – Jeremy

+1

Tôi tin rằng vấn đề là bạn đang cố in một tệp . Mã trên yêu cầu tệp chỉ chứa mã PCL và bạn muốn gửi trực tiếp tới máy in mà không cần cửa sổ can thiệp. Nói cách khác, bạn dường như đang cố gắng in tệp một tệp đã được in vào một tệp. –

Các vấn đề liên quan