c# - 从 PrintPreview 创建的图形是纵向而不是横向?

标签 c# winforms print-preview

我正在用 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/

相关文章:

html - 需要测试 IE8 的打印功能

c# - Assembly Resolve递归成无限循环

c# - 通过 JWT 身份验证在 gRPC C# 中填充 AuthContext

sql - 查找 VB.net 应用程序中使用的表以删除未使用的表

c# - Form.ShowDialog() 并处理

c++ - 如何在Win32 C++中进行打印预览?

c# - 自动换行时的 OpenXML、PresentationML 表格高度和行高

c# - 在 QueryBuilder 中使用 or 运算符连接 where 查询

c# - 单击“确定”按钮后如何关闭当前表单并将字符串传递给预制件

javascript - 仅在页 footer 分打印最后一页底部的表页脚