我的打印方法保存 XPS 文件但不打印?

发布于 2024-10-16 14:25:41 字数 749 浏览 8 评论 0原文

我的代码导致 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖粟与秋泊 2024-10-23 14:25:41

您的“Microsoft XPS Document Writer”打印机必须设置为默认打印机。

It must be that your "Microsoft XPS Document Writer" printer is setup as your default printer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文