代码如下:
StringBuilder sb=null; public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
} public class PrintDirect
{
[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", CharSet = CharSet.Unicode, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
ref DOCINFO pDocInfo); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long StartPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode,
SetLastError=true, ExactSpelling=true)]
public static extern bool WritePrinter(IntPtr hPrinter, byte[] pBytes, int dwCount, ref int dwWritten); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long EndPagePrinter(IntPtr hPrinter); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long EndDocPrinter(IntPtr hPrinter); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long ClosePrinter(IntPtr hPrinter);
} private void button1_Click(object sender, EventArgs e)
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=db1.mdb"; OleDbConnection objConnection = new OleDbConnection(strConnection); objConnection.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select data from print where ID=1", objConnection);
DataSet ds = new DataSet();
da.Fill(ds, "print"); printStr = ds.Tables["print"].Rows[0]["data"].ToString();
//给文档换行,在70个字符后换行
for (i = 0; i < printStr.Length - 70; i += 70)
sb.Append(printStr.Substring(i, 70)).Append("\n");
sb.Append(printStr.Substring(i)).Append("\n"); System.IntPtr lhPrinter = new System.IntPtr(); DOCINFO di = new DOCINFO();
int pcWritten = 0; //用Encoding转换字符串
byte[] bytStr = System.Text.Encoding.Default.GetBytes(sb.ToString()); PrintDirect.OpenPrinter("\\\\192.168.112.5\\1150PCL 5e", ref lhPrinter, 0);
PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
PrintDirect.StartPagePrinter(lhPrinter); try
{
PrintDirect.WritePrinter(lhPrinter, bytStr, printStr.Length, ref pcWritten);
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
} PrintDirect.EndPagePrinter(lhPrinter);
PrintDirect.EndDocPrinter(lhPrinter);
PrintDirect.ClosePrinter(lhPrinter);
}
现在可以打印,1页的word文档,只能打印出开头三行和最后一行,并且是分两张纸出来的急求解决方案,项目到期,非常着急!求顶,UP散分
StringBuilder sb=null; public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
} public class PrintDirect
{
[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", CharSet = CharSet.Unicode, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
ref DOCINFO pDocInfo); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long StartPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode,
SetLastError=true, ExactSpelling=true)]
public static extern bool WritePrinter(IntPtr hPrinter, byte[] pBytes, int dwCount, ref int dwWritten); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long EndPagePrinter(IntPtr hPrinter); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long EndDocPrinter(IntPtr hPrinter); [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern long ClosePrinter(IntPtr hPrinter);
} private void button1_Click(object sender, EventArgs e)
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=db1.mdb"; OleDbConnection objConnection = new OleDbConnection(strConnection); objConnection.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select data from print where ID=1", objConnection);
DataSet ds = new DataSet();
da.Fill(ds, "print"); printStr = ds.Tables["print"].Rows[0]["data"].ToString();
//给文档换行,在70个字符后换行
for (i = 0; i < printStr.Length - 70; i += 70)
sb.Append(printStr.Substring(i, 70)).Append("\n");
sb.Append(printStr.Substring(i)).Append("\n"); System.IntPtr lhPrinter = new System.IntPtr(); DOCINFO di = new DOCINFO();
int pcWritten = 0; //用Encoding转换字符串
byte[] bytStr = System.Text.Encoding.Default.GetBytes(sb.ToString()); PrintDirect.OpenPrinter("\\\\192.168.112.5\\1150PCL 5e", ref lhPrinter, 0);
PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
PrintDirect.StartPagePrinter(lhPrinter); try
{
PrintDirect.WritePrinter(lhPrinter, bytStr, printStr.Length, ref pcWritten);
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
} PrintDirect.EndPagePrinter(lhPrinter);
PrintDirect.EndDocPrinter(lhPrinter);
PrintDirect.ClosePrinter(lhPrinter);
}
现在可以打印,1页的word文档,只能打印出开头三行和最后一行,并且是分两张纸出来的急求解决方案,项目到期,非常着急!求顶,UP散分
sb.Append(printStr.Substring(i, 70)).Append("\n");
这里有问题吧,感觉一致在换行啊,没有跳出