printdocument
时间: 2023-08-14 19:06:25 浏览: 90
`PrintDocument` is a class in C# that represents a document that can be printed. It provides properties and methods to define the content of the document, such as the font, color, and layout. To use `PrintDocument`, you need to create an instance of the class, set its properties, and then call its `Print` method to send the document to the printer. Here is an example:
```csharp
private void buttonPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
pd.Print();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
// Define the content of the document
string text = "Hello, world!";
Font font = new Font("Arial", 12);
Brush brush = Brushes.Black;
Point point = new Point(100, 100);
// Draw the content on the page
e.Graphics.DrawString(text, font, brush, point);
}
```
This code creates a `PrintDocument` instance, sets its `PrintPage` event handler to the `printDocument1_PrintPage` method, and then calls its `Print` method. In the `printDocument1_PrintPage` method, it defines the content of the document (in this case, a string of text) and then draws it on the page using the `Graphics.DrawString` method.
阅读全文