无对话框自动打印

时间:2013-07-11 10:21:54

标签: c#

我找到了打印代码。但我想在没有对话框的情况下自动发送到打印机。我知道printername。我从SQL表中获取打印机名称。我该怎么做 ?

// 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");

3 个答案:

答案 0 :(得分:3)

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

using System.Drawing.Printing;

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

循环浏览可用的打印机名称:

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

答案 1 :(得分:1)

我删除if (pd.ShowDialog() != true) return;并添加此代码pd.PrintQueue = new PrintQueue(new PrintServer(), "printer name" );以打印我选择的打印机。

答案 2 :(得分:0)

如果您想直接发送到default打印机(就是这样,without必须在上面的代码中提供其名称),则可以使用以下代码:

private void PrintSimpleTextButton_Click(object sender, RoutedEventArgs e)
    {
        // Create a PrintDialog  
        PrintDialog printDlg = new PrintDialog();

        // Create a FlowDocument dynamically.  
        FlowDocument doc = CreateFlowDocument();
        doc.Name = "FlowDoc";

        // Create IDocumentPaginatorSource from FlowDocument  
        IDocumentPaginatorSource idpSource = doc;

        // Call PrintDocument method to send document to printer  
        printDlg.PrintDocument(idpSource.DocumentPaginator, "Hello WPF Printing.");
    }

    /// <summary>  
    /// This method creates a dynamic FlowDocument. You can add anything to this  
    /// FlowDocument that you would like to send to the printer  
    /// </summary>  
    /// <returns></returns>  
    private FlowDocument CreateFlowDocument()
    {
        // Create a FlowDocument  
        FlowDocument doc = new FlowDocument();

        // Create a Section  
        Section sec = new Section();

        // Create first Paragraph  
        Paragraph p1 = new Paragraph();

        // Create and add a new Bold, Italic and Underline  
        Bold bld = new Bold();
        bld.Inlines.Add(new Run("First Paragraph"));
        Italic italicBld = new Italic();
        italicBld.Inlines.Add(bld);

        Underline underlineItalicBld = new Underline();
        underlineItalicBld.Inlines.Add(italicBld);

        // Add Bold, Italic, Underline to Paragraph  
        p1.Inlines.Add(underlineItalicBld);

        // Add Paragraph to Section  
        sec.Blocks.Add(p1);

        // Add Section to FlowDocument  
        doc.Blocks.Add(sec);
        return doc;
    }