Auto print without dialog

后端 未结 3 1727
情话喂你
情话喂你 2020-12-06 03:04

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 ?



        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 03:26

    If you'd like to send directly to your default printer (that's it, without having to give its name in the above code) you can use this piece:

    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.");
        }
    
        ///   
        /// This method creates a dynamic FlowDocument. You can add anything to this  
        /// FlowDocument that you would like to send to the printer  
        ///   
        ///   
        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;
        }
    

提交回复
热议问题