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

本文关键字:打印 对话框 显示 何打印 | 更新日期: 2023-09-27 17:51:15

我为大学创建了一个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 ";
    }
}

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

由于您将问题标记为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

中查看本教程
  • Creating-print-preview-page-dynamically-in-ASP-NET