根据documentation,添加这些代码应该会增加打印文档的边距,但是当我在代码中使用它时,我看不到任何边距被添加。我对代码的用法正确吗?这是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();

这是我的代码:
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();
}

最佳答案

没有边距影响文档的原因是我没有将OriginAtMargins的值更改为true。因此,您需要将其从PrintDocument()更改为:

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

07-26 09:36