を使用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()
。これはまったく正しい考えではないようです。