Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

c# - Auto print without dialog

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");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) {
  //
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...