本文介绍了自动打印无对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了打印代码。但是我想自动发送到打印机而没有对话框。我知道打印机名称。我从SQL表获得打印机名称。我该怎么做 ?

I found code for printing. But I want to sent to printer automaticaly without dialog box. I know printername. I get printer name from SQL table. How can I do it ?

// select printer and get printer settings
        PrintDialog pd = new PrintDialog();
        if (pd.ShowDialog() != true) return;


        // create a document
        FixedDocument document = new FixedDocument();
        document.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

        // create a page
        FixedPage page1 = new FixedPage();
        page1.Width = document.DocumentPaginator.PageSize.Width;
        page1.Height = document.DocumentPaginator.PageSize.Height;

        // add some text to the page
        TextBlock page1Text = new TextBlock();
        page1Text.Text = "This is a text"
        page1Text.FontSize = 12; // 30pt text
        page1Text.Margin = new Thickness(50); // 1 inch margin
        page1.Children.Add(page1Text);

        // add the page to the document
        PageContent page1Content = new PageContent();
        ((IAddChild)page1Content).AddChild(page1);
        document.Pages.Add(page1Content);

        // and print
        pd.PrintDocument(document.DocumentPaginator, "Print");


推荐答案

而不是PrintDialog类,请尝试使用PrintDocument类直接,您可以在其中设置打印机的名称:

Instead of the PrintDialog class, try using the PrintDocument class directly, where you can set the printer by the name:

using System.Drawing.Printing;

PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "my printer";

要遍历可用的打印机名称:

To loop through the available printer names:

foreach (string s in PrinterSettings.InstalledPrinters) {
  //
}

这篇关于自动打印无对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:16
查看更多