Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DefaultPageSettings.Margins not adding margins

Based from the documentation, adding these codes should add margin to the print document, but when I used it in my codes, I can't see any margin being added. Is my usage on the code correct? Here is the code from MSDN:

 printFont = new Font("Arial", 10);
 PrintDocument pd = new PrintDocument(); 
 pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
 pd.PrinterSettings.PrinterName = printer;
 Margins margins = new Margins(100,100,100,100);
 pd.DefaultPageSettings.Margins = margins;
 pd.Print();

Here is my code:

printDoc = new PrintDocument();
PrinterSettings printSettings = new PrinterSettings();
PaperSize paperSize = new PaperSize("Receipt", 350, 700);

Margins margin = new Margins(2000, 1000, 2000, 1000);
printDoc.DefaultPageSettings.PaperSize = paperSize;

printDoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = printDoc;
DialogResult result = printPreview.ShowDialog();
printDoc.DefaultPageSettings.Margins = margin;
if (result == DialogResult.OK)
{
    printDoc.Print();
}
like image 451
Carl Binalla Avatar asked Mar 09 '17 07:03

Carl Binalla


1 Answers

The reason why there are no margins affecting the document is because I didn't change the value of OriginAtMargins to true. So you need to change it from the PrintDocument(), like this:

New printDoc = new PrintDocument();
printDoc.OriginAtMargins = true; //Default is false
like image 126
Carl Binalla Avatar answered Oct 27 '22 01:10

Carl Binalla