C# C打印Y位置单独改变

C# C打印Y位置单独改变,c#,printing,printdocument,C#,Printing,Printdocument,我正试图创建一种收据。发生的情况是,我没有打印机的驱动程序,也不存在打印机的操作系统。因此,我只安装了一台通用的文本打印机。我的打印机是POS打印机之一 这是我的配置: int _x = 10; int _y = 5; int _width = 270; // max width I found through trial and error int _height = 0; StringFormat _center = new StringFormat(); StringFormat _le

我正试图创建一种收据。发生的情况是,我没有打印机的驱动程序,也不存在打印机的操作系统。因此,我只安装了一台通用的文本打印机。我的打印机是POS打印机之一

这是我的配置:

int _x = 10;
int _y = 5;
int _width = 270; // max width I found through trial and error
int _height = 0;

StringFormat _center = new StringFormat();
StringFormat _left = new StringFormat();
StringFormat _right = new StringFormat();

_center.Alignment = StringAlignment.Center;
_left.Alignment = StringAlignment.Near;
_right.Alignment = StringAlignment.Far;

PrintDocument _print = new PrintDocument();
PrintPreviewDialog _printPrev = new PrintPreviewDialog();

_print.DefaultPageSettings.PaperSize = new PaperSize("My Custom Receipt", _width, 600);
_print.DefaultPageSettings.PaperSize.RawKind = (int)PaperKind.Custom;
_print.DefaultPageSettings.Landscape = false;
_print.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

_print.OriginAtMargins = true;

_printPrev.Document = _print;
_printPrev.PrintPreviewControl.AutoZoom = false;
_printPrev.PrintPreviewControl.Zoom = 1;
在我使用的纸上写下:

_print.PrintPage += (Object sender, PrintPageEventArgs e) => {

    // Header information
    e.Graphics.DrawString("Number:", small, Brushes.DarkGray, new Rectangle(_x, _y, _width, _height), _left);
    e.Graphics.DrawString(number.ToString(), medium, Brushes.Black, new Rectangle(_x, _y, _width, _height), _center);
    _y += (int)e.Graphics.MeasureString(number.ToString(), medium).Height;

    e.Graphics.DrawString("BARCODE:", small, Brushes.DarkGray, new Rectangle(_x, _y, _width, _height), _left);
    e.Graphics.DrawString(barcode.ToString(), medium, Brushes.Black, new Rectangle(_x, _y, _width, _height), _center);
    _y += (int)e.Graphics.MeasureString(barcode.ToString(), medium).Height;

    // And so on..
}
发生的情况是,如果我只写两行,例如,所有这些行都保持+/-粘性到标题中,否则,它们开始被推入底部。下图说明了该问题:

解决了

我所要做的就是将以下变量复制到inside\u print.PrintPage

int _x = 10;
int _y = 5;
int _width = 270; // max width I found through trial and error
int _height = 0;