2016-11-16 74 views
1

有找到纸张中心坐标的方法吗?c#打印等边距

我已经阅读了许多讨论,论坛和提示 - 我无法做到这一点。 林印像这样:

 using (PrintDialog pd = new PrintDialog()) 
    { 
     if (pd.ShowDialog() != DialogResult.OK) return; 
     PrintDocument document = new PrintDocument(); 
     document.PrinterSettings = pd.PrinterSettings; 
     document.PrintPage += new PrintPageEventHandler(Document_PrintPage); 
     document.Print(); 
    } 

    private void Document_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     int X = (int)e.PageSettings.PrintableArea.X; 
     int Y = (int)e.PageSettings.PrintableArea.Y; 
     int width = (int)e.PageSettings.PrintableArea.Width - X; 
     int height = (int)e.PageSettings.PrintableArea.Height - Y; 

     int centerX = (width - X)/2 + X; 
     int centerY = (height - Y)/2 + Y; 

     e.Graphics.DrawRectangle(Pens.Gray, new Rectangle(X, Y, width, height)); 
     e.Graphics.DrawLine(Pens.Black, centerX - 100, centerY, centerX + 100, centerY); 
     e.Graphics.DrawLine(Pens.Black, centerX, centerY - 100, centerX, centerY + 100); 
    } 

当然我试图计算在很多方面这些坐标,包括使用MarginBounds,Hardmargins的,保证金等

任何人都知道如何打印的东西究竟在一张中心?

回答

0

e.PageBounds是你在找什么(属性PrintPageEventArgs)。它为您提供了代表页面总面积的矩形区域。

int centerX = e.PageBounds.Width/2; 
int centerY = e.PageBounds.Height/2;