1

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

3 回答 3

4

我有完全相同的问题,最终发现了这个。添加 OnQueryPageSettings 委托处理程序。

void OnQueryPageSettings(object obj,QueryPageSettingsEventArgs e)
{
    if (e.PageSettings.PrinterSettings.LandscapeAngle != 0)
        e.PageSettings.Landscape = true;            
}

并到您的 PrintDocument

prnDoc.QueryPageSettings += new QueryPageSettingsEventHandler(OnQueryPageSettings);

那为我修好了。

于 2012-03-24T12:08:32.760 回答
1

我有同样的问题。但是,如果我以正确的宽度和高度绘制页面内容(即交换它们),一切都会正常工作。

int width = dc.VisibleClipBounds.Width;
int height = dc.VisibleClipBounds.Height;
if(width < height)
{
    int temp = width;
    width = height;
    height = temp;
}

然后根据宽度和高度绘制页面内容。

不是最好的解决方案,但可以确保我们始终绘制到横向页面。

于 2014-03-20T11:49:29.580 回答
0

我找不到在哪里连接 David Bolton 的解决方案,但找到了另一种方法。

http://wieser-software.blogspot.co.uk/2012/07/landscape-printing-and-preview-in-wpf.html

从根本上说,您需要在 DocumentPaginator 的 GetPage 方法返回的每个 DocumentPage 上设置 PageSize。

于 2012-07-25T10:37:15.607 回答