我正在使用PrintDocument.Print()
启动打印过程,其中我打印数据网格 (C1FlexGrid) 和一些页眉和页脚信息。这是一个有点复杂的打印过程。我正在使用标准PrintDocument
方法,但由于我想要点击页面,我可以控制正在发生的一切。
我遇到的问题是我想缩小将绘制网格控件的区域。当我绘制页眉和页脚时,我正在计算它们将占用的空间,以及网格应该占用的空间。网格控件有它自己的PrintDocumentGridRenderer
类,该类提供了PrintPage()
我调用的方法,以使其在 PrintDocument 的Graphics
对象上呈现网格。
我无法弄清楚如何限制网格可以容纳的区域,但是在我已经绘制页眉/页脚并知道剩余空间是什么之后再做。
这是一些代码,我认为是本质:
private void PrintDocument_PrintPage(Object sender, PrintPageEventArgs e)
{
//I tried putting a non-drawing version of DrawHeadersAndFooters() here to get the calculated space and then reset the Margin...but it's always one call behind the Graphics object, meaning that it has no effect on the first page. In fact, because Setup() gets called with two different margins at that point, the pages end up very badly drawn.
_gridRenderer.Setup(e); //this is the PrintDocumentGridRender object and Setup() figures out page layout (breaks and such)
DrawHeadersAndFooters(e.Graphics, e.MarginBounds);
Int32 newX = _printProperties.GridBounds.X - e.MarginBounds.X;
Int32 newY = _printProperties.GridBounds.Y - e.MarginBounds.Y;
e.Graphics.TranslateTransform(newX, newY);
_gridRenderer.PrintPage(e, _currentPage - 1); //grid control's print method
e.HasMorePages = _currentPage < _printProperties.Document.PrinterSettings.ToPage;
_currentPage++;
}
private void DrawHeadersAndFooters(Graphics graphics, Rectangle marginBounds)
{
Rectangle textRect = new Rectangle();
Int32 height = 0;
//loop lines in header paragraph to get total height required
//there are actually three, across the page, but just one example for bevity...
if (!String.IsNullOrEmpty(_printProperties.HeaderLeft))
{
Int32 h = 0;
foreach (String s in _printProperties.HeaderLeft.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
h += (Int32)graphics.MeasureString(s, _printProperties.HeaderLeftFont, width, stringFormat).Height;
height = (h > height) ? h : height;
} //repeat for other two, keeping the greatest of 3 heights in the end
textRect.X = marginBounds.X;
textRect.Y = (Int32)_printProperties.Document.DefaultPageSettings.PrintableArea.Y; //a global storage for printing information I need to keep in memory
textRect.Width = width;
textRect.Height = height;
stringFormat.Alignment = StringAlignment.Near;
graphics.DrawString(_printProperties.HeaderLeft, _printProperties.HeaderLeftFont, new SolidBrush(_printProperties.HeaderLeftForeColor), textRect, stringFormat);
_printProperties.GridBounds = new Rectangle(marginBounds.X, textRect.Y, marginBounds.Width, marginBounds.Bottom - textRect.Y); //here I think I have the space into which the grid _should_ be made to fit
}
您可以看到,PrintDocument_PrintPage()
我正在对Graphics
对象应用变换,它将网格向下移动到标题下方。
截屏:
所以,问题:
我想自下而上缩小该区域,以使该网格的底部刚好在页脚上方。您可以通过查看右下角看到渲染的网格图像与我已经绘制的页脚重叠。这就是我需要的帮助。如何在Graphics
不做类似的事情的情况下缩小绘图空间ScaleTransform()
,这似乎根本不是正确的想法。