9

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");
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
user2550719
  • 119
  • 1
  • 2
  • 5

3 Answers3

8

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) {
  //
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
2

I remove if (pd.ShowDialog() != true) return; and add this code pd.PrintQueue = new PrintQueue(new PrintServer(), "printer name" ); to print my choose printer.

user2550719
  • 119
  • 1
  • 2
  • 5
0

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.");
    }

    /// <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;
    }
Paul Efford
  • 261
  • 4
  • 12