我正在尝试打印扩展名为.txt的文档,为此,我指的是我的课程书,但感到困惑。这是书中给出的源代码:
private void Print_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1 // there is no object in my program names ad printDocument1
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
还有更多的代码
最佳答案
您的示例假设已将PrintDocument对象从工具箱拖动到表单上:尽管可以轻松地自己创建对象。
private void Print_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
PrintDialog printDialog = new PrintDialog
{
Document = printDocument
};
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print(); // Raises PrintPage event
}
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(...);
}
关于c# - 在C#中打印文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4803759/