//以下是我做一个打印标签的程序 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Printing; using System.IO; using System.Drawing.Text; using System.Xml;namespace TestPrint { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button_Print_Click(object sender, EventArgs e) { nowCount = 0; nCount = 0; //获得设置后的属性 stuctStamp tempStamp = new stuctStamp(); printDoc = new PrintDocument(); printDialog.Document = printDoc; if (printDialog.ShowDialog() != DialogResult.OK) { return; }
try { string strFile = this.textBox_Open_File.Text; string line; streamToPrint = new StreamReader(strFile,System.Text.Encoding.Default); alData = new System.Collections.ArrayList(); while ((line = streamToPrint.ReadLine()) != null) { tempStamp = GetStructStamp(line); // this.progressBar.Value += line.Length; // nCount += myStamp.nCount; for (int i = 0; i < tempStamp.nCount; i++) { alData.Add(tempStamp); } } //this.progressBar.Maximum = streamToPrint.ReadToEnd().Length; this.progressBar.Maximum = alData.Count; this.progressBar.Step = 1; this.progressBar.Minimum = 0; this.progressBar.Value = 0; streamToPrint.Close(); kk = 0; //alData取数索引 // streamToPrint = new StreamReader(strFile,System.Text.Encoding.Default); printDoc.PrintPage += new PrintPageEventHandler(PrintDoc_PrintPage); if (printDoc.PrinterSettings.IsValid) { printDoc.DefaultPageSettings.Landscape = false; // printDoc.OriginAtMargins = false;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using System.Drawing.Text;
using System.Xml;namespace TestPrint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button_Print_Click(object sender, EventArgs e)
{
nowCount = 0;
nCount = 0;
//获得设置后的属性
stuctStamp tempStamp = new stuctStamp();
printDoc = new PrintDocument();
printDialog.Document = printDoc;
if (printDialog.ShowDialog() != DialogResult.OK)
{
return;
}
try
{
string strFile = this.textBox_Open_File.Text;
string line;
streamToPrint = new StreamReader(strFile,System.Text.Encoding.Default);
alData = new System.Collections.ArrayList();
while ((line = streamToPrint.ReadLine()) != null)
{
tempStamp = GetStructStamp(line);
// this.progressBar.Value += line.Length;
// nCount += myStamp.nCount;
for (int i = 0; i < tempStamp.nCount; i++)
{
alData.Add(tempStamp);
}
}
//this.progressBar.Maximum = streamToPrint.ReadToEnd().Length;
this.progressBar.Maximum = alData.Count; this.progressBar.Step = 1;
this.progressBar.Minimum = 0;
this.progressBar.Value = 0;
streamToPrint.Close(); kk = 0; //alData取数索引
// streamToPrint = new StreamReader(strFile,System.Text.Encoding.Default);
printDoc.PrintPage += new PrintPageEventHandler(PrintDoc_PrintPage);
if (printDoc.PrinterSettings.IsValid)
{
printDoc.DefaultPageSettings.Landscape = false;
// printDoc.OriginAtMargins = false;
printDoc.Print();
}
else
{
MessageBox.Show("Printer is invalid.");
}
}
catch
{
}
finally
{
}
}
private void PrintDoc_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.HasMorePages = true; int RowsPerPage = 0; //每页的行数
int ColumsPerPage = 0; //每页的列数
ev.Graphics.PageUnit = GraphicsUnit.Display; //百分之一英寸 //不要边界.默认为1英寸= 2.54厘米
//系统默认边界为1英寸 //printDoc.OriginAtMargins = false;
ev.PageSettings.Margins.Left = 0;
ev.PageSettings.Margins.Bottom = 0;
ev.PageSettings.Margins.Right = 0;
ev.PageSettings.Margins.Top = 0; //页大小
int pageWidth = (int)(float.Parse(this.textBox_PageWidth.Text) / 10 / 2.54 * 100);
int pageHeight = (int)(float.Parse(this.textBox_PageHeight.Text) / 10 / 2.54 * 100); //页的边距
int pageLeftMargin = (int)(float.Parse(this.textBox_PageLeftMagin.Text) / 10 / 2.54 * 100);
int pageTopMargin = (int)(float.Parse(this.textBox_PageTopMargin.Text) / 10 / 2.54 * 100);
int leftSite = (int)(float.Parse(this.textBox_LeftMagin.Text) / 10 / 2.54 * 100);
int topSite = (int)(float.Parse(this.textBox_TopMargin.Text) / 10 / 2.54 * 100); //一个标签的大小
int nWidth = (int)(float.Parse(this.textBox_Width.Text) / 10 / 2.54 * 100);
int nHeight = (int)(float.Parse(this.textBox_Height.Text) / 10 / 2.54 * 100); //计数器
int nCol = 0;//
int nRow = 0; // string strFont = this.comboBox_Font.Text;
string[] strArr = strFont.Split(new char[] { ',' });
Font printFont = new Font(strArr[0], int.Parse(strArr[1]), System.Drawing.FontStyle.Bold, GraphicsUnit.Point);//以毫米作为单位 Brush printBrush = Brushes.Black; //跨度
int lineHei = (int)(printFont.GetHeight(ev.Graphics)); // between class and code // MessageBox.Show("索书号跨度" + lineHei.ToString()); int xSite = 0;
int ySite = 0; // MessageBox.Show("打印纸宽度" + (ev.MarginBounds.Width).ToString() + "打印纸高度" + (ev.MarginBounds.Height).ToString()); //以百分之一英寸为单位的矩形区域,表示边距以内的页面部分。
RowsPerPage = (int)((ev.PageBounds.Height) / nHeight);
ColumsPerPage = (int)((ev.PageBounds.Width) / nWidth); // MessageBox.Show("每行个数" + ColumsPerPage.ToString() + "每页行数" + RowsPerPage.ToString());
while ((nRow < RowsPerPage) && (kk < alData.Count))
{
//打印个数计数
//myStamp = this.GetStructStamp(line);
this.progressBar.Value = kk;
myStamp = (stuctStamp)alData[kk];
nCount++;
//MessageBox.Show("当前标签" + myStamp.nCount + "个"); nowCount++;
// MessageBox.Show("当第" + nowCount.ToString() + "个标签 当前行" + nRow.ToString()); ySite = pageTopMargin + nRow * nHeight;
//横坐标变换
xSite = pageLeftMargin + nCol * nWidth; //开始打印一个标签
ev.Graphics.DrawString(myStamp.strClass, printFont, printBrush, new Point(xSite + leftSite, ySite + topSite), new StringFormat());
ev.Graphics.DrawString(myStamp.strCode, printFont, printBrush, new Point(xSite + leftSite + 1, ySite + topSite + lineHei), new StringFormat()); nCol++;
kk ++;
if (Mod(nowCount, ColumsPerPage) == 0)
{
{
nRow++;
nCol = 0;
}
}
}
// MessageBox.Show("当前个数" + nCount.ToString());
if (kk < alData.Count)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
//定义标签结构
struct stuctStamp
{
public string strClass; //分类号 905$d
public string strCode; //索书号 905$e
public int nCount; //个数 可以拆分905$b 或 905$f
}
}
}