日期:2014-05-18  浏览次数:20937 次

用API打印问题:为什么只能打选取的.txt文件,不能打word,excel,html等,我是向打印机输出byte[]啊
选取.txt文本文件打没有问题,但是选取word等文件打出来得就是乱码!!我是向打印机输出byte[]啊。。为什么。。

请高手指点。


using System;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace XXL.Tools
{

  public class PrintingUtil
  {

  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  struct DOCINFOW
  {
  [MarshalAs(UnmanagedType.LPWStr)]
  public string pDocName;
  [MarshalAs(UnmanagedType.LPWStr)]
  public string pOutputFile;
  [MarshalAs(UnmanagedType.LPWStr)]
  public string pDataType;
  }

  [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false,
  CallingConvention = CallingConvention.StdCall)]
  public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault);

  //[DllImport("winspool.Drv", EntryPoint = "OpenPrinterW",
  // CharSet = CharSet.Unicode, SetLastError = true,
  // ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  //static extern bool OpenPrinter(string src, ref IntPtr hPrinter, long pd);

  [DllImport("winspool.Drv", EntryPoint = "ClosePrinter",
  CharSet = CharSet.Unicode, SetLastError = true,
  ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  static extern bool ClosePrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterW",
  CharSet = CharSet.Unicode, SetLastError = true,
  ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  static extern bool StartDocPrinter(IntPtr hPrinter,
  int level, ref DOCINFOW pDI);

  [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter",
  CharSet = CharSet.Unicode, SetLastError = true,
  ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  static extern bool EndDocPrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter",
  CharSet = CharSet.Unicode, SetLastError = true,
  ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  static extern bool StartPagePrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter",
  CharSet = CharSet.Unicode, SetLastError = true,
  ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  static extern bool EndPagePrinter(IntPtr hPrinter);

  [DllImport("winspool.Drv", EntryPoint = "WritePrinter",
  CharSet = CharSet.Unicode, SetLastError = true,
  ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  static extern bool WritePrinter(IntPtr hPrinter,
  IntPtr pBytes, int dwCount, ref int 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 or False on failure.
  public bool Sen