Printing does not start at the top edge of the page

.net, c#, printing, winforms

Solution

You have to set the `PrintDocument.OriginAtMargins` property to true to consider your margins.

From MSDN,

`When OriginAtMargins is true, the Graphics object location takes into account the PageSettings.Margins property value and the printable area of the page`

But printing from the exact edge depends on the printable area which is defined by the physical limitations of the printing device. Check the `HardMarginX` and `HardMarginY` to get the physical origin of the printer. For more information refer the answer of this question.

Problem

I am trying to print some `string`s using `Graphicss.DrawString()`. I have set margins to the `printdocument` but does not start from the origin of the page. I have set `margins` to `(0,0,0,0)` but somehow it prints half centimeter below the top edge of the page. Another thing is that it can print from left edge. Below is my code. ``` private void button1_Click(object sender, EventArgs e) { ////PaperSize pkCustomSize1 = new PaperSize("First custom size", 1020, 3517); ////printDocument1.DefaultPageSettings.PaperSize = pkCustomSize1; printPreviewDialog1.Document = printDocument1; printDocument1.PrinterSettings.PrinterName = this.comboBox1.Text; Margins margins = new Margins(0, 0, 0, 0); printDocument1.PrinterSettings.DefaultPageSettings.Margins = margins; printPreviewDialog1.Show(); printDocument1.Print(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { int resX = GetPrinterResolutionX(comboBox1); int resY = PrnOpra.GetPrinterResolutionY(comboBox1); Graphics g = e.Graphics; float scale = resX / ScrnRes; Bitmap bm = new Bitmap(367, 1205); g.DrawRectangle(new Pen(Color.Black, 0.5F), panel9.Location.X / 2, panel9.Location.Y / 2, panel9.Width, panel9.Height); g.DrawImage(bm, 0, 0); } ``` What's wrong with code?

Original source

Related problems