我正在用 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();
}
然后 PrintPreview
对话框请求打印时调用 Print_Me
函数:
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
最佳答案
我遇到了完全相同的问题,并最终找到了这个。添加 OnQueryPageSettings 委托(delegate)处理程序。
void OnQueryPageSettings(object obj,QueryPageSettingsEventArgs e)
{
if (e.PageSettings.PrinterSettings.LandscapeAngle != 0)
e.PageSettings.Landscape = true;
}
和您的 PrintDocument
prnDoc.QueryPageSettings += new QueryPageSettingsEventHandler(OnQueryPageSettings);
这对我来说已经解决了。
关于c# - 从 PrintPreview 创建的图形是纵向而不是横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7917564/