7

我找到了打印代码。但我想在没有对话框的情况下自动发送到打印机。我知道打印机名称。我从 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");
4

3 回答 3

6

代替 PrintDialog 类,尝试直接使用 PrintDocument 类,您可以在其中通过名称设置打印机:

using System.Drawing.Printing;

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

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

foreach (string s in PrinterSettings.InstalledPrinters) {
  //
}
于 2013-07-11T12:14:26.880 回答
2

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

于 2013-07-11T12:21:52.580 回答
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;
    }
于 2020-01-21T13:55:18.377 回答