c# - 如何防止在执行打印预览时出现“打印进度”对话框

标签 c# .net printing system.drawing

在我的 C# 应用程序中,我试图在屏幕上不显示进度对话框的情况下生成打印预览。

我相信你可以使用 PrintDocument.PrintController以防止在真实打印时出现这种情况(即不是打印预览),但是在执行打印预览时它似乎不起作用。

我的代码如下:

public FrmDeliveryNotePrintPreview(DeliveryNote deliveryNote)
{
    InitializeComponent();

    this.Text = "Delivery Note #" + deliveryNote.Id.ToString();


    // The print preview window should occupy about 90% of the
    // total screen height

    int height = (int) (Screen.PrimaryScreen.Bounds.Height * 0.9);


    // Making an assumption that we are printing to A4 landscape,
    // then adjust the width to give the correct height:width ratio
    // for A4 landscape.

    int width = (int) (height / 1.415);


    // Set the bounds of this form. The PrintPreviewControl is
    // docked, so it should just do the right thing

    this.SetBounds(0, 0, width, height);

    PrinterSettings printerSettings = new PrinterSettings();
    PrintDeliveryNotes pdn = new PrintDeliveryNotes(
        new DeliveryNote[] { deliveryNote },
        printerSettings);
    PrintDocument printDocument = pdn.PrintDocument;
    printDocument.PrintController = new PreviewPrintController();
    ppcDeliveryNote.Document = printDocument;
}

除了显示打印预览进度对话框之外,打印预览完全符合我的要求。

有什么建议吗?

最佳答案

这对我有用:

将文档的 printcontroller 设置为 StandardPrintController

static class Program
    {

        static void Main()
        {
            PrintDocument doc = new PrintDocument();
            doc.PrintController = new StandardPrintController();
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);

            doc.Print();
        }

        static void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString("xxx", Control.DefaultFont, Brushes.Black, new PointF(e.PageBounds.Width / 2, e.PageBounds.Height / 2));
        }
    }

关于c# - 如何防止在执行打印预览时出现“打印进度”对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1357271/

相关文章:

c# - 当返回类型为 IHttpActionResult 时,Web API 2 返回不带引号的简单字符串

java - 解析来自 .Net 网络服务的 JSON 响应

c# - ConcurrentDictionary Keys 或 Values 属性是线程安全的

java - 反向打印给定数组

c# - 使用 Moq 安排单元测试

c# - 如何以面向 future 的方式从 SPA 正确获取 IDS4 中的 token ?

c# - 如何使用 javascript 运行 C# 命令?

c# - 多线程环境下的异常处理

java - 使用Outfile打印,程序执行错误

从没有 'while' 的数据库中打印 PHP