You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过System.Printing将PS文件发送至打印池避免缺纸等打印异常

问题描述

我此前通过TCP协议向打印机发送打印任务,但当打印机出现缺墨、缺纸等状况时,TCP连接失败会抛出异常,导致打印任务直接失败。我希望通过将打印任务加入系统打印池来规避该问题,尝试了基于System.Printing的示例代码但未成功。

测试代码
using System.Linq;
using System.Windows.Forms;
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Printing;

namespace WindowFormApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.detail();
        }
        private void detail()
        {
            getSystemPrinters();
        }
        private void getSystemPrinters()
        {
            // 创建本地打印服务器
            LocalPrintServer printServer = new LocalPrintServer();

            // 从可用打印队列中选择打印机
            PrintQueue selectedPrintQueue = SelectPrintQueue(printServer.GetPrintQueues());

            if (selectedPrintQueue != null )
            {
                SpotTroubleUsingProperties(out String statusReport, selectedPrintQueue);
                if (statusReport == null)
                {
                    // 打印PS文件
                    PrintPSFile(selectedPrintQueue, @"C:\dhl_digi\\RawPrintServer\Spool\Fail\1.ps");
                    //PrintPSFile(selectedPrintQueue, @"C:\dhl_digi\DocGateway\GF_OCR_PDF\1.xps");
                }
                else
                {
                    // 处理打印机错误
                }
               
            }

            // 释放打印服务器资源
            printServer.Dispose();
        }

        // 从可用打印队列中选择指定打印机
        public static PrintQueue SelectPrintQueue(PrintQueueCollection printQueues)
        {
            foreach (PrintQueue printQueue in printQueues)
            {
                if (printQueue.FullName.Equals("CI PRINTER"))
                {
                    return printQueue;
                }
             
            }

            return null; // 未找到指定打印机
        }

        // 向指定打印队列发送PS文件打印任务
        public static void PrintPSFile(PrintQueue pq, string psFilePath)
        {
            FileInfo f = new FileInfo(psFilePath);
            string fullname = f.FullName;

            byte[] file = File.ReadAllBytes(fullname);

            using (var job = pq.AddJob())
            using (var stream = job.JobStream)
            {
                stream.Write(file, 0, file.Length);
            }
        }

        // 检查打印机的异常状态
        public static void SpotTroubleUsingProperties(out String statusReport, PrintQueue pq)
        {
            statusReport = null;
            if (pq.HasPaperProblem)
            {
                statusReport += "存在纸张问题。 ";
            }
            if (!pq.HasToner)
            {
                statusReport += "墨粉耗尽。 ";
            }
            if (pq.IsDoorOpened)
            {
                statusReport += "机门未关闭。 ";
            }
            if (pq.IsInError)
            {
                statusReport += "处于错误状态。 ";
            }
            if (pq.IsNotAvailable)
            {
                statusReport += "不可用。 ";
            }
            if (pq.IsOffline)
            {
                statusReport += "处于离线状态。 ";
            }
            if (pq.IsOutOfMemory)
            {
                statusReport += "内存不足。 ";
            }
            if (pq.IsOutOfPaper)
            {
                statusReport += "缺纸。 ";
            }
            if (pq.IsOutputBinFull)
            {
                statusReport += "出纸槽已满。 ";
            }
            if (pq.IsPaperJammed)
            {
                statusReport += "纸张卡住。 ";
            }
            if (pq.IsPaused)
            {
                statusReport += "已暂停。 ";
            }
            if (pq.IsTonerLow)
            {
                statusReport += "墨粉不足。 ";
            }
            if (pq.NeedUserIntervention)
            {
                statusReport += "需要用户干预。 ";
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Yeong Lestle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 14:32:51