111,129
社区成员
发帖
与我相关
我的任务
分享
public class PrintCase
{
private float LineHeight = 0f;
private const int Lines = 13;
private LinePointInfo[] LinesPoints = new LinePointInfo[13];
private PrintDocument prDoc = null;
private Font prFont = null;
private readonly int StringTop = 8;
public PrintCase()
{
this.prDoc = new PrintDocument();
this.prDoc.PrintPage += new PrintPageEventHandler(this.prDoc_PrintPage);
}
private void prDoc_PrintPage(object sender, PrintPageEventArgs e)
{
//
//TODO: 在这里计算要打印内容的位置
float x = 0f;
float y = 0f;
this.prFont = new Font("华文新魏", 18f, FontStyle.Bold);
x = e.MarginBounds.Left + ((e.MarginBounds.Width - (this.szCaption.Length * this.prFont.Size)) / 4f);
y = this.prFont.GetHeight(e.Graphics);
e.Graphics.DrawString("CompanyCaption", this.prFont, Brushes.Black, x, y, new StringFormat());
x += 60f;
y += Convert.ToSingle((double) (this.prFont.GetHeight(e.Graphics) * 1.5));
e.Graphics.DrawString("szCaption", this.prFont, Brushes.Black, x, y, new StringFormat());
x += 200f;
this.prFont = new Font("MS UI Gothic", 18f, FontStyle.Bold);
e.Graphics.DrawString(WebSiteCaption, this.prFont, Brushes.Black, x, y, new StringFormat());
Font font = new Font("华文细黑", 16f, FontStyle.Bold);
//
e.HasMorePages = false;
}
public readonly string strConfigFile = Application.StartupPath + "\\PrintConfig.xml";
public void Print()
{
string strPrinterName = @"\\ZUOXI04\EPSON LQ-300K+ ESC/P 2";//打印机名称
PrinterSettings settings = new PrinterSettings();
settings.PrinterName = strPrinterName;
PageSettings defaultPageSettings = this.prDoc.DefaultPageSettings;
foreach (PaperSize size in settings.PaperSizes)
{
if (size.PaperName == "Custom Format")
{
defaultPageSettings.PaperSize = size;
break;
}
}
defaultPageSettings.PrinterSettings = settings;
this.prDoc.PrinterSettings = settings;
this.prDoc.DefaultPageSettings = defaultPageSettings;
this.prDoc.Print();
}
public void ShowPreview() //打印预览
{
PrintPreviewDialog dialog = new PrintPreviewDialog();
dialog.Document = this.prDoc;
dialog.ShowDialog();
}
[StructLayout(LayoutKind.Sequential)]
private struct LinePointInfo
{
public PointF LeftTop;
public PointF LeftBottom;
public PointF RightTop;
public PointF RightBottom;
public void Init()
{
this.LeftTop = new PointF(0f, 0f);
this.LeftBottom = new PointF(0f, 0f);
this.RightTop = new PointF(0f, 0f);
this.RightBottom = new PointF(0f, 0f);
}
}
}