(1)自定义纸张设置
控制面板->打印机和传真->右键->服务器属性->创建新的格式
(2)自定义纸张使用
this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("NewPrint", iWidth, iHeight);
NewPrint:制定一纸张名称。 iWidth:纸张使用宽度。 iHeight:纸张使用高度。
iWidth,iHeight 可以在使用过程中调整。
例如:iWidth=923,iHeight=480
(3)ESC/P指令使用
using System;
using System.Runtime.InteropServices;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace PrintDome
{
class ClsPrintLPT
{
private IntPtr iHandle;
private FileStream fs;
private StreamWriter sw;
private string prnPort = "LPT1"; //打印机端口
public ClsPrintLPT()
{
}
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
///
/// 打开一个vxd(设备)
///
[DllImport("kernel32.dll", EntryPoint = "CreateFile", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes,
int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
///
/// 开始连接打印机
///
private bool PrintOpen()
{
iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
MessageBox.Show("没有连接打印机或者打印机端口不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
else
{
fs = new FileStream(iHandle, FileAccess.ReadWrite);
sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据
return true;
}
}
///
/// 打印字符串
///
/// 要打印的字符串
private void PrintLine(string str)
{
sw.WriteLine(str); ;
}
///
/// 关闭打印连接
///
private void PrintEnd()
{
sw.Close();
fs.Close();
}
///
/// 打印票据
///
/// tb_Temp 全部字段数据集合
/// true:打印成功 false:打印失败
public bool PrintDataSet(DataSet dsPrint)
{
try
{
if (PrintOpen())
{
PrintLine(" ");
PrintLine("[XXXXXXXXXXXXXXXXXX超市]"