- // Place 2 buttons on the form (PrintPreview, Print)
- // PLace a textbox control on the form (TextData)
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Printing;
- using System.Text;
- using System.Windows.Forms;
- namespace PrintPreviewDialogExample
- {
- public partial class Form1 : Form
- {
- private PrintDocument Document;
- public Form1()
- {
- InitializeComponent();
- }
- private void PrintPreview_Click(object sender, EventArgs e)
- {
- Document = new PrintDocument();
- PrintPreviewDialog PrintPreviewDlg = new PrintPreviewDialog();
- PrintPreviewDlg.Document = Document;
- PrintPreviewDlg.Show();
- Document.PrintPage += new PrintPageEventHandler(Document_PrintPage);
- }
- private void Print_Click(object sender, EventArgs e)
- {
- Document.Print();
- }
- void Document_PrintPage(object sender, PrintPageEventArgs e)
- {
- Graphics g = e.Graphics;
- SolidBrush Brush = new SolidBrush(Color.Black);
- g.DrawString(this.TextData.Text, new Font("arial", 9), Brush, 10, 10);
- }
- }
- }
C# - Simple PrintDocument Example
This code snippet shows how to use the PrintDocument class to print documents. It uses the PrintPreviewDialog to show the preview of the document a button to print the document.
No comments:
Post a Comment