如何打印和打印预览对话框显示?

时间:2013-01-11 06:24:52

标签: c#

我为大学创建了一个Web应用程序。我想在学生的打印结果上添加打印和打印预览按钮。哪些类用于打印表单?

我的打印和打印预览按钮编码如下:

protected void btnprint_Click(object sender, EventArgs e)
{
    if (this.gridcontrol != null)
    {
        GridPrintDocument gpd = new GridPrintDocument(this.gridcontrol, true);

        PrintDialog pritdlg = new PrintDialog();
        pritdlg.Document = gpd;

        if (pritdlg.ShowDialog() == DialogResult.OK)
            gpd.Print();
    }
    else
    {
        lblstatus.Text="An error occurred attempting to print the grid ";
    }
}

protected void btnprntprew_Click(object sender, EventArgs e)
{
    if (this.gridcontrol != null)
    {
        //Uses the default printer.
        GridPrintDocument gpd = new GridPrintDocument(this.gridcontrol, true);
        PrintPreviewDialog pripredlg = new PrintPreviewDialog();
        pripredlg.Document = gpd;
        pripredlg.ShowDialog();
    }
    else
    {
        lblstatus.Text = "An error occurred attempting to preview the grid ";
    }
}

1 个答案:

答案 0 :(得分:1)

当您将问题标记为C#时,所以在C#printDialog和printPreviewDialog中可以选择执行哪些代码。

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

           e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);  //richtextbox1 contains what you want to print

           e.Graphics.PageUnit = GraphicsUnit.Inch;          

}

private void btnPrint_Click(object sender, EventArgs e)

{

            //PrintDialog associate with PrintDocument;

            printDialog1.Document = printDocument1;

            if (printDialog1.ShowDialog()==DialogResult.OK)
            {
                printDocument1.Print();
            }
}

private void btnPrintPreview_Click(object sender, EventArgs e)
{
       //Associate PrintPreviewDialog with PrintDocument.

        printPreviewDialog1.Document = printDocument1;  

        // Show PrintPreview Dialog

        printPreviewDialog1.ShowDialog();
}

您可以在Asp.net中看到本教程是否需要它