/// ------------------------------------------------------------------------------------ /// <summary> /// Sets the margins in the specified print context. The PrintPageEventArgs contains /// a rectangle of the printable part of the page. Hence, the difference between the /// page's rectangle and the margin's rectangle gives you the size of the margins. /// However, the print context object wants the margins in terms of the size of the /// gap between the edge of the paper and it's nearest margin. For example, if there are /// one inch margins all around, the print context wants all its margins set to one /// inch. /// </summary> /// <param name="e"></param> /// ------------------------------------------------------------------------------------ private void SetMargins(PrintPageEventArgs e) { int dpiX = (int)e.Graphics.DpiX; int dpiY = (int)e.Graphics.DpiY; Rectangle margins = e.MarginBounds; Rectangle page = e.PageBounds; Rectangle printable = new Rectangle( (int)e.Graphics.VisibleClipBounds.Left, (int)e.Graphics.VisibleClipBounds.Top, (int)e.Graphics.VisibleClipBounds.Width, (int)e.Graphics.VisibleClipBounds.Height); // Get the top and left unprintable areas. For many printers that's a value // between 0.25 and 0.5 inches. IntPtr hdc = e.Graphics.GetHdc(); int unprintableLeft = GetDeviceCaps(hdc, PHYSICALOFFSETX); int unprintableTop = GetDeviceCaps(hdc, PHYSICALOFFSETY); e.Graphics.ReleaseHdc(hdc); // To be useful, the printable rectangle needs to be offset so it indicates // the actual part of the page where we can print. The offsets we got above // are in pixels, convert to 100ths of an inch like the other coords we have here. printable.Offset(unprintableLeft * 100 / dpiX, unprintableTop * 100 / dpiY); m_vwPrintContext.SetMargins( PixelsFrom100ths(margins.Left - printable.Left, dpiX), PixelsFrom100ths(printable.Right - margins.Right, dpiX), PixelsFrom100ths((margins.Top - printable.Top) / 2, dpiY), // heading; get from smarter page setup? PixelsFrom100ths(margins.Top - printable.Top, dpiY), // top PixelsFrom100ths(printable.Bottom - margins.Bottom, dpiY), // bottom PixelsFrom100ths((printable.Bottom - margins.Bottom) / 2, dpiY)); // footer; get from smarter page setup? }
private void SetMargins(PrintPageEventArgs e) { int dpiX = (int)e.Graphics.DpiX; int dpiY = (int)e.Graphics.DpiY; Rectangle margins = e.MarginBounds; Rectangle page = e.PageBounds; bool landscape = e.PageSettings.Landscape; Rectangle printable = new Rectangle( (int)e.Graphics.VisibleClipBounds.Left, (int)e.Graphics.VisibleClipBounds.Top, (int)e.Graphics.VisibleClipBounds.Width, (int)e.Graphics.VisibleClipBounds.Height); // To be useful, the printable rectangle needs to be offset so it indicates // the actual part of the page where we can print. printable.Offset((int)(e.PageSettings.HardMarginX), (int)(e.PageSettings.HardMarginY)); Rectangle relative; if (MiscUtils.IsUnix) { dpiX = 72; dpiY = 72; if (landscape) { page = new Rectangle(e.PageBounds.Y, e.PageBounds.X, e.PageBounds.Height, e.PageBounds.Width); } relative = page; } else { relative = printable; } m_vwPrintContext.SetMargins( PixelsFrom100ths(margins.Left - relative.Left, dpiX), PixelsFrom100ths(relative.Right - margins.Right, dpiX), PixelsFrom100ths((margins.Top - relative.Top) / 2, dpiY), // heading; get from smarter page setup? PixelsFrom100ths(margins.Top - relative.Top, dpiY), // top PixelsFrom100ths(relative.Bottom - margins.Bottom, dpiY), // bottom PixelsFrom100ths((relative.Bottom - margins.Bottom) / 2, dpiY)); // footer; get from smarter page setup? }