DefaultPageSettings.Margins不添加边距

时间:2017-03-09 07:37:22

标签: c# wpf margins printdocument

根据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();
}

1 个答案:

答案 0 :(得分:8)

没有影响文档的边距的原因是因为我没有将OriginAtMargins的值更改为true。所以你需要从PrintDocument()更改它,如下所示:

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