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

2024-02-25

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

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

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

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

copy filename.pcl //location/printername

所以我想我也可以以编程方式做同样的事情(使用副本)...我想知道是否有更干净的方法来做到这一点,比如使用 PrintDocument。

请注意,当我使用 PrintDocument 时:

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

pd.Print();

我总是打印出一张空白页。


很抱歉这么晚才回答这个问题,但我有一些代码可以完成这项工作。原来不是我写的。我从另一个程序员的帮助网站收到了代码,但我不记得是哪一个了。它工作得很漂亮。这是创建 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 字体。它用代码包装字体,为其分配字体 ID 32545,然后调用 dll 函数 SendStringToPrinter。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C# 中打印 PCL 文件? 的相关文章

随机推荐

  • C 的 GCD 函数

    Q 1 问题5 可整除 我尝试了蛮力法 但是需要时间 所以我参考了几个网站 找到了这段代码 include
  • ChartJS 甜甜圈图表渐变填充

    因此 我尝试为 ChartJS 圆环图进行渐变填充 但这仅适用于水平方向 而不适用于圆形 这是我正在使用的代码 var ctx document getElementById chart area getContext 2d var gra
  • 仅显示 shell_exec('df') 中磁盘使用数据的特定列

    我正在尝试编写一个 PHP 脚本来执行用于报告的 shell 函数 我从磁盘使用报告开始 我想要以下格式 drive path total size free space 没有其他的 我的脚本是 output shell exec df h
  • 我可以在 TCPDF 中使用“旧式”(非衬里)数字吗?

    Unicode 不区分衬里数字 与大写字母具有相同的比例 在表格中很有用 但在运行文本中很突出 和非衬里数字 它们看起来更像小写字母 具有上升部分和下降部分 因为它认为它们是彼此的变体 不过 许多字体都具有两组数字 并提供了一种在它们之间进
  • Mozilla firefox 无法使用 window.onbeforeunload

    我在用着window onbeforeunload在窗口关闭时向用户显示消息 该功能在 Chrome 和 IE 上运行良好 但在 Firefox 上不起作用 我使用的是 Firefox 版本26 0我已经尝试了很多 但没有任何意义 有人说这
  • 如何通过解耦的后端和前端进行社交身份验证(Passport / Express / React)

    我正在尝试使用 PassportJS Express 后端和 React JS 前端来进行社交身份验证 但是 我不确定如何去做 我做了一些阅读并实现了社交身份验证 当使用 Google Auth 登录时 它会返回由 Express 应用程序
  • 是否建议在 bash 脚本中捕获 SIGPIPE?

    我在使用系统调用命令从 C 执行 bash 脚本时遇到问题 该脚本捕获了一个SIGPIPE发出信号并退出并返回代码141 这个问题只在我的代码的最后一个版本中开始出现 我的问题如下 为什么这个 SIGPIPE 现在出现而以前没有出现 忽略
  • 带脚本的文本编辑器...适用于 Linux

    一段时间以来 我一直在我的 Windows 机器上使用 UltraEdit 事实证明 使用熟悉的语言 JavaScript 编写脚本的能力非常有用 唯一的问题是我无法在工作时在我的 Linux 机器上使用它 是否有在 Linux 上运行并具
  • 如何“解码”UTF-8 字符?

    假设我想编写一个函数来比较两个 Unicode 字符 我该怎么做呢 我读了一些文章 比如this http en wikipedia org wiki UTF 8 但还是没明白 让我们来 作为输入 已经在范围内了0x0800 and 0xF
  • WIA服务2、在windows xp/7上下载并安装

    我编写了一个从扫描仪扫描图像的应用程序 这在我的开发机器 win7 Ultimate sp1 64位 上运行良好 我尝试在 Windows XP 计算机 或 Windows Server 2008 标准 上运行该应用程序 但失败并出现此错误
  • Android IllegalArgumentException:如果应用程序在后台运行一段时间,则状态类错误

    如果我设置应用程序背景 我认为这是由于内存不足造成的 日志如下 java lang RuntimeException Unable to start activity ComponentInfo com qingdaonews bus co
  • 标头包含深度限制[重复]

    这个问题在这里已经有答案了 我想知道 包含头文件时 包含文件的深度可以无限增加吗 你能在编译时指定一个限制吗 Example main c include A h const int xyz CONST VALUE A h include
  • MapView 没有从 ViewPager 中删除?

    在我的应用程序中 我试图在 ViewPager 内实现地图视图 我的应用程序中有 4 个不同的页面 MapView在第四页 我确实成功加载了地图 但是当我滑回第一页时 必须使用 destroyItem 方法销毁第四个视图 如果我刷到第四页
  • 如何在云监控/stackdriver中按状态显示总dataproc作业?

    Dataproc 作业中应该有成功 失败 待处理状态 当然我可以在 Cloud Console 上 Dataproc 下的作业部分中看到该状态 但是 如何在云监控 stackdriver 中可视化所有这些状态 已经尝试过记分卡图表并使用指标
  • 是否可以从包含操作系统的 .img 文件创建 docker 映像

    是否可以转换 img包含操作系统 Arch Linux 的文件到 Docker 镜像中 更准确地说我想要码头化RuneAudio Raspberry Pi 图像 从完整的操作系统映像生成 Docker 映像通常是一个次优的过程 操作系统映像
  • HTML 注释行为

    所以 我在 Magento WYSIWYG 编辑器 所有东西 中闲逛时注意到呈现为在生成的 HTML 中 似乎也将任何字符串括在呈现正常的评论 我只在 Chrome 中测试过这一点 但这种行为对我来说似乎有点奇怪 我看过W3C 规范评论 h
  • 文本词云绘制错误

    我有以下用于绘制词云的代码 并且收到后续错误 wordcloud dm word dm freq scale c 8 2 min freq 2 max words Inf random order FALSE rot per 15 colo
  • 在 Windows 上为 python 2.7 安装 gstreamer 1.0。

    我一直在尝试在 Windows 上安装 gstreamer 1 0 以用作 python 2 7 模块 我从这里安装了sdkhttp docs gstreamer com display GstSDK Installing on Windo
  • Tensorflow 错误“形状 Tensorshape() 必须具有等级 1”

    import tensorflow as tf import numpy as np import os from PIL import Image cur dir os getcwd def modify image image resi
  • 如何在 C# 中打印 PCL 文件?

    我有一个使用 打印到文件 生成的 PCL 文件 在 C 中以编程方式打印此文件的最佳方法是什么 当然 考虑到我要打印的打印机支持 PCL 我知道我可以通过从提示中调用来进行打印 copy filename pcl location prin