c#以相同的边距打印

时间:2016-11-16 19:58:35

标签: c# .net visual-studio c#-4.0 c#-3.0

有没有办法找到纸张中心的坐标?

我读过许多讨论,论坛和提示 - 我无法做到。 我打印像这样:

     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,Margins等。

任何人都知道如何在纸质中心打印一些东西?

1 个答案:

答案 0 :(得分:0)

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

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