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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With