c# - 使用套接字打印到IP地址和字节数组在某些打印机上打印空白页

标签 c# sockets pdf printing

有没有人有向打印机发送字节数组的经验?我将pdf文件直接发送到打印机的IP地址,它在某些打印机上打印,而在其他打印机上打印空白页。这是打印机驱动程序问题吗?这是打印的代码:

 public class PrintService : IPrintService
  {
    ManualResetEvent connectDone;
    ManualResetEvent sendDone;

    /// <inheritDoc />
    public void PrintData(byte[] data, string printerName)
    {
      connectDone = new ManualResetEvent(false);
      sendDone = new ManualResetEvent(false);
      IPAddress ip = IPAddress.Parse(printerName);
      IPEndPoint remoteEP = new IPEndPoint(ip, 9100);
      Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      client.NoDelay = true;

      try
      {
        client.BeginConnect(remoteEP, new AsyncCallback(connectCallback), client);
        connectDone.WaitOne();
        client.BeginSend(data, 0, data.Length, 0, new AsyncCallback(sendCallback), client);
        sendDone.WaitOne();
      }
      finally
      {
        // Shutdown the client
        this.shutDown(client);
      }
    }

    private void connectCallback(IAsyncResult ar)
    {
      // Retrieve the socket from the state object.
      Socket client = (Socket)ar.AsyncState;

      // Complete the connection.
      client.EndConnect(ar);

      // Signal that the connection has been made.
      connectDone.Set();
    }
    private void sendCallback(IAsyncResult ar)
    {
      // Retrieve the socket from the state object.
      Socket client = (Socket)ar.AsyncState;

      // Complete sending the data to the remote device.
      int bytesSent = client.EndSend(ar);

      // Signal that all bytes have been sent.
      sendDone.Set();
    }
    private void shutDown(Socket client)
    {
      client.Shutdown(SocketShutdown.Both);
      client.Close();
    }
  }

最佳答案

因此,在尝试了其他所有类的感觉(PrintDocumentPrintServerProcessInfoadobe/foxit)之后,将文件直接复制到打印机,使用DLLImport[Kernetl32.dll]在端口上创建文件,使用GCHandlepointers,我将注意力集中在与打印机设置。对于特定的打印机,我有以下这些步骤可以使我的原始代码正常工作:

  • 关闭打印机
  • 握住打印机上的复选标记(通常位于清晰可见的位置)和右箭头键(在我的情况下位于复选标记旁边),然后等待....填满屏幕并消失
  • 将基本(不是网络)设置恢复为默认设置(不确定是否必须执行此步骤,因为下一步是在什么时候起作用了)
  • 禁用PPDS(个人打印机数据流http://en.wikipedia.org/wiki/Personal_Printer_Data_Stream)
  • 关于c# - 使用套接字打印到IP地址和字节数组在某些打印机上打印空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27926649/

    相关文章:

    javascript - 确认消息框返回错误值

    c# - MVC 项目中的 jQuery 错误 : Datepicker is not a function

    c# - 无效方差 : The type parameter 'T' must be contravariantly valid on 'UserQuery.IItem<T>.ItemList' . 'T' 是协变的

    sockets - Log4J Appender UDP

    python - 使用多个查询时套接字响应时间变慢

    javascript - 根据组ID向d3可视化添加分页符

    C# HttpClient : Why MediaTypeWithQualityHeaderValue?

    Ruby - 如何在 Prawn 中使用不同的字体?

    r - 如何检查PDF是否是扫描图像或包含R中的文本

    Java 套接字绑定(bind)错误