C# 在WPF中使用PrintDocument

C# 在WPF中使用PrintDocument,c#,wpf,C#,Wpf,如何在WPF中使用以下代码 private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString("Name", new Font("tahoma", 10), Brushes.Black, 100, 100); e.Graphics.DrawString("Last", new Font("tahoma", 10), Brush

如何在WPF中使用以下代码

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawString("Name", new Font("tahoma", 10), Brushes.Black, 100, 100);
        e.Graphics.DrawString("Last", new Font("tahoma", 10), Brushes.Black, 100, 120);
    }

由于某些原因,我还无法发表评论。不过,您展示的是添加到eventhandler的方法,下面是我制作的将文本打印到文档中的示例:

将方法添加到eventhandler:

 printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage);
eventhandler方法(您发布的案例):

立即打印:

 printDocument.Print();

我的代码在WINDOWSFORMAPPLICATION中使用,所以我可以在WPF中使用您的代码吗?我认为WPF不支持“Graphics.DrawString”我只回答了如何使用您发布的方法,这些是system.drawing的一部分,专门用于打印。我以为你想从你的wpf打印application@HenrikBøgelundLavstsen他想知道如何在WPF中而不是在Windows窗体中使用。@SHINJaeGuk这应该对两者都有效,它处理打印,实际上它与UI没有真正的关系,它只是.net,所以它应该对两者都有效。
 printDocument.Print();