我的打印方法保存 XPS 文件但不打印?
我的代码导致 saveFileDialog 保存 .xps 文件,而不是实际打印任何内容。我的代码有问题吗?如果没有,我该如何配置我的机器才能使此代码工作?
class Printer {
public Printer() {
}
private void PrintDocument_PrintPage(Object sender, PrintPageEventArgs e) {
Font printFont = new Font("Courier New", 12);
e.Graphics.DrawString(StringToPrint, printFont, Brushes.Black, 0, 0);
}
private PrintDocument pd = new PrintDocument();
private string StringToPrint;
public void PrintString(string input) {
pd.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
StringToPrint = input;
pd.Print();
}
}
代码:
Printer Printer = new Printer();
Printer.PrintString("Hello world");
My code results in a saveFileDialog to save a .xps file instead of actually printing anything. Is there something wrong with my code? If not, how can I configure my machine to make this code work?
class Printer {
public Printer() {
}
private void PrintDocument_PrintPage(Object sender, PrintPageEventArgs e) {
Font printFont = new Font("Courier New", 12);
e.Graphics.DrawString(StringToPrint, printFont, Brushes.Black, 0, 0);
}
private PrintDocument pd = new PrintDocument();
private string StringToPrint;
public void PrintString(string input) {
pd.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
StringToPrint = input;
pd.Print();
}
}
Code:
Printer Printer = new Printer();
Printer.PrintString("Hello world");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的“Microsoft XPS Document Writer”打印机必须设置为默认打印机。
It must be that your "Microsoft XPS Document Writer" printer is setup as your default printer.