0

I created a web application for college. I want to add print and print preview button to printing result of student. Which classes are used for printing a form?

My print and print preview button coding is below:

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
  • "PrintDialog" is WinForms in my opinion. How does this match with your statement that you are creating a Web application? Commented Jan 11, 2013 at 7:17

1 Answer 1

1

As you tagged your question as C#,so in C# printDialog and printPreviewDialog is the option to do the work which code will be..

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();
}

You can see this tutorial whether you want it in Asp.net

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.