gpt4 book ai didi

c# - 如何在 C# 中打印 PCL 文件?

转载 作者:可可西里 更新时间:2023-11-01 08:26:37 24 4
gpt4 key购买 nike

我有一个使用“打印到文件”生成的 PCL 文件。

在 C# 中以编程方式打印此文件的最佳方法是什么?

(当然,我要打印的打印机支持 PCL。)

我知道我可以通过从提示中调用来打印:

copy filename.pcl //location/printername

所以我想我也可以通过编程方式做同样的事情(使用复制)...我想知道是否有更简洁的方法来做到这一点,比如使用 PrintDocument。

请注意,当我使用 PrintDocument 时:

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

pd.Print();

我总是打印空白页。

最佳答案

很抱歉这么晚才回答这个问题,但我有一些代码可以完成这项工作。原来不是我写的。我从另一个程序员的帮助站点收到代码,但我不记得是哪一个。它工作得很漂亮。这是创建一个 dll 的项目。要自己动手,选择 Create:/Project/Class Library(在项目类型下选择 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 字体。它用代码包装字体,为其分配字体 ID 32545,然后调用 dll 函数 SendStringToPrinter。

关于c# - 如何在 C# 中打印 PCL 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3507187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com