Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintDocument always has margin

I am having a problem when using PrintDocument with margins.

No matter what I do there is always a margin around everything I print, this means that nothing is aligned where it needs to be.

Here is the code I am using to create the PrintDocument

public void Print()
{
   PrintDocument printDocument = new PrintDocument();
        printDocument.DefaultPageSettings.PaperSize = new PaperSize("A5",583,827);
        printDocument.OriginAtMargins = true;
        printDocument.DefaultPageSettings.Margins.Top = 0;
        printDocument.DefaultPageSettings.Margins.Left = 0;
        printDocument.DefaultPageSettings.Margins.Right = 0;
        printDocument.DefaultPageSettings.Margins.Bottom = 0;

        if (!string.IsNullOrWhiteSpace(PrinterName))
        {
            printDocument.PrinterSettings.PrinterName = PrinterName;
        }

        printDocument.PrintController = new StandardPrintController();
        printDocument.PrintPage += On_PrintPage;
        printDocument.Print();
}

The On_PrintPage method, has various calls to e.Graphics.Draw... methods.

How can I make it so that something I print at 0,0 will print in the very top left of the page. I know that if the printer cannot print that far to the edge of the page it will be blank, however it should do that rather than print 0,0 not in the top left of the page.

I'm really lost here

like image 627
Nick Williams Avatar asked Jan 27 '14 15:01

Nick Williams


1 Answers

interestingly the print function is too late to set most of the properties and would only apply to subsequent pages

you need to use PrintDocument.QueryPageSettings event instead and set the properties there and I always set the page settings not just defaults. then drawing at 0,0 should be as close as you can get (printer + driver allows)

like image 109
Stephen Symonds Avatar answered Oct 20 '22 23:10

Stephen Symonds