我正在用 C# 打印自定义页面。实际打印文档时,它可以正常工作,将其显示到对话框中(通过相同的代码)也是如此。当代码用于PrintPreview
对话框时,页面以横向模式显示,但Graphics
创建的页面具有纵向文档的尺寸,因此预览无法正确显示。这是我正在使用的代码的精简版本
using (PrintDocument pd = new PrintDocument())
{
pd.PrinterSettings.PrintToFile = false;
pd.DefaultPageSettings.Landscape = true;
pd.PrinterSettings.DefaultPageSettings.Landscape = true;
pd.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
PrintDialog pDialog = new PrintDialog();
pDialog.Document = pd;
pDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
pDialog.PrinterSettings.PrintToFile = false;
pDialog.Document.DefaultPageSettings.Landscape = true;
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = pd;
printPreview.ShowDialog();
}
然后当对话框请求打印Print_Me
时调用一个函数:PrintPreview
private void Print_Me(object sender, PrintPageEventArgs e)
{
using (Graphics g = e.Graphics)
{
DrawToDC(g);
e.HasMorePages = hasMorePages;
}
}
在DrawToDC
我使用以下内容来获取尺寸,正如我所提到的,这些尺寸适用于实际打印和显示到对话框:
dc.VisibleClipBounds.Width
dc.VisibleClipBounds.Height