在 C# 中显示打印预览

Posted

技术标签:

【中文标题】在 C# 中显示打印预览【英文标题】:Showing Print Preview in C# 【发布时间】:2013-10-19 18:08:13 【问题描述】:

现在,我正在尝试在 PrintDocument 上构建我的表单,但让我查看内容实际出现在页面上的唯一方法是打印一张纸。它有效,但我有很多东西需要添加,而且我不想浪费大量的纸张。现在我有一个打印对话框出现,但没有打印预览按钮。有没有办法让我出现?谢谢!

public partial class Form4 : System.Windows.Forms.Form
    
        private System.ComponentModel.Container components;
        private System.Windows.Forms.Button printButton;

        public Form4()
        
            // The Windows Forms Designer requires the following call.
            InitializeComponent();
        

        // The Click event is raised when the user clicks the Print button. 
        private void printButton_Click(object sender, EventArgs e)
        
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            PrintDialog pdi = new PrintDialog();
            pdi.Document = pd;
            if (pdi.ShowDialog() == DialogResult.OK)
            
                pd.Print();
            

        

        // The PrintPage event is raised for each page to be printed. 
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        
            Single yPos = 0;
            Single leftMargin = e.MarginBounds.Left;
            Single topMargin = e.MarginBounds.Top;
            Image img = Image.FromFile("logo.bmp");
            Rectangle logo = new Rectangle(40, 40, 50, 50);
            using (Font printFont = new Font("Arial", 20.0f))
            
                e.Graphics.DrawImage(img, logo);
                e.Graphics.DrawString("Header", printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
            
            using (SolidBrush blueBrush = new SolidBrush(Color.Black))
            
                Rectangle rect = new Rectangle(100, 100, 500, 120);
                e.Graphics.FillRectangle(blueBrush, rect);
            
        


        // The Windows Forms Designer requires the following procedure. 
        private void InitializeComponent()
        
            this.components = new System.ComponentModel.Container();
            this.printButton = new System.Windows.Forms.Button();

            this.ClientSize = new System.Drawing.Size(504, 381);
            this.Text = "Print Example";

            printButton.ImageAlign =
               System.Drawing.ContentAlignment.MiddleLeft;
            printButton.Location = new System.Drawing.Point(32, 110);
            printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            printButton.TabIndex = 0;
            printButton.Text = "Print the file.";
            printButton.Size = new System.Drawing.Size(136, 40);
            printButton.Click += new System.EventHandler(printButton_Click);

            this.Controls.Add(printButton);
        

    

【问题讨论】:

我确定您使用的是 Windows 窗体,因此请务必使用 winforms 标签标记您的问题。为此,您可以使用PrintPreviewControl。 如果想要在打印前有一个单独的按钮来预览您的文档,请查看此How do I trigger the PrintPreviewDialog 您可能希望忽略文件流方面。对我有帮助,希望对其他人也有帮助 【参考方案1】:

您可以为此使用PrintPreviewDialog

private void printButton_Click(object sender, EventArgs e)

    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

    PrintDialog printdlg = new PrintDialog();
    PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();

    // preview the assigned document or you can create a different previewButton for it
    printPrvDlg.Document = pd;
    printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below

    printdlg.Document = pd;

    if (printdlg.ShowDialog() == DialogResult.OK)
    
        pd.Print();
    

【讨论】:

注意:PrintPreviewDialog 是 System.Windows.Forms 命名空间的一部分。

以上是关于在 C# 中显示打印预览的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中打印和打印预览位图

C# Winform想做打印表单功能,但是打印预览总是显示空白页,请Winform大神指教,不用连接数据库

c#调用bartender打印时不显示打印预览吗

C# winform做打印预览和打印功能怎么做?

C#实现打印与打印预览功能

C#教程之打印和打印预览