DefaultPageSettings.Margins does not add fields

Based on the documentation , adding these codes should add margin to the print document, but when I used it in my codes, I can don’t add the added margin. Am I using the code correctly? 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();
}
+4
source share
1 answer

The reason for the absence of fields affecting the document is that I did not change the value OriginAtMarginsto true. So you need to change it from PrintDocument(), for example:

New printDoc = new PrintDocument();
printDoc.OriginAtMargins = true; //Default is false
+4
source

Source: https://habr.com/ru/post/1671859/


All Articles