C# Print Dialog Box


using System;
using System.Drawing;
using System.Windows.Forms;
namespace Win
dowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintDialog dlg = new PrintDialog();
dlg.ShowDialog();
}
}
}
How to print a Document in C#?

PrintDocument object represents a document to be printed. It encapsulates all the information needed to print a page. They associate with the control which content can be print. They handle the events and operations of printing. Once a PrintDocument is created, we can set the Document property of PrintDialog as this document. After that we can also set other properties.
privatevoid PrintButton_Click(object sender, EventArgs e) {
PrintDialog pDlg = newPrintDialog();
PrintDocument pDoc = newPrintDocument();
pDoc.DocumentName = "Print Document";
pDlg.Document = pDoc;
pDlg.AllowSelection = true;
pDlg.AllowSomePages = true;
if (pDlg.ShowDialog() == DialogResult.OK)
{
pDoc.Print();
}
else
{
MessageBox.Show("Print Cancelled");
}
}
How to Format PrintDocument?
If you want to set the size of the paper:
printDialog.Document = printDocument;
printDocument.DefaultPageSettings.PaperSize = new PaperSize("my Document", width, height);
printDocument.DefaultPageSettings.Landscape = true;
How to Preview PrintDocument
With the help of PrintPreviewDialog You can preview a document also.
printPreviewDialog1.Document = printDocument; //Associate PrintPreviewDialog with PrintDocument.
printPreviewDialog1.ShowDialog(); // Show PrintPreview Dialog
Related Topics
- C# Visual Studio IDE
- How to Create a C# Windows Forms Application
- C# Label Control
- C# Button Control
- C# TextBox Control
- C# ComboBox
- C# ListBox Control
- C# Checked ListBox Control
- C# RadioButton Control
- C# CheckBox Control
- C# PictureBox Control
- C# ProgressBar Control
- C# ScrollBars Control
- C# DateTimePicker Control
- C# Treeview Control
- C# ListView Control
- C# Menu Control
- C# MDI Form
- C# Color Dialog Box
- C# Font Dialog Box
- C# OpenFile Dialog Box
- keyPress event in C# , KeyDown event in C# , KeyUp event in C#
- How to create Dynamic Controls in C# ?
- Keep Form on Top of All Other Windows
- C# Timer Control