I'm trying to create a sort of a receipt. What happens is that I don't have the driver (nor exists for my OS) of my printer. So I've installed a Generic Printer Text Only. My printer is one of the POS printers.
This is my configuration:
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;
And to write into the paper I use:
_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..
}
What happens is that if I only write two lines (for example) all of that lines stay +/- sticky into the header, otherwise, they start to being pushed into the bottom. The following images illustrates the problem:
Solved.
All I had to do was to copy the following variables to inside _print.PrintPage()
.
int _x = 10;
int _y = 5;
int _width = 270; // max width I found through trial and error
int _height = 0;