0

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

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

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 ";
    }
}
4

1 回答 1

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 中查看本教程是否需要它

于 2013-01-11T07:30:31.270 回答