6

I'm trying MSDN's example of printing using PrintDocument, but it's not going so well. I've got it all to compile, but when I hit print, a "Fax Sending Settings" window pops up. Is this supposed to happen? Im trying to print, not send a fax!

What would I have to change to print this straight to the default printer instead?

Thanks!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;

namespace WindowsFormsApplication1
{
public partial class Form4 : System.Windows.Forms.Form
{
    private System.ComponentModel.Container components;
    private System.Windows.Forms.Button printButton;
    private Font printFont;
    private StreamReader streamToPrint;

    public Form4()
    {
        // The Windows Forms Designer requires the following call.
        InitializeComponent();
    }

    // The Click event is raised when the user clicks the Print button. 
    private void printButton_Click(object sender, EventArgs e)
    {
         PrintDocument pd = new PrintDocument();
         pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
         pd.Print();

    }

    // The PrintPage event is raised for each page to be printed. 
    private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        Single yPos = 0;
        Single leftMargin = e.MarginBounds.Left;
        Single topMargin = e.MarginBounds.Top;
        Image img = Image.FromFile("logo.bmp");
        Rectangle logo = new Rectangle(40, 40, 50, 50);
        using (Font printFont = new Font("Arial", 10.0f))
        {
            e.Graphics.DrawImage(img, logo);
            e.Graphics.DrawString("Testing!", printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
        }
    }


    // The Windows Forms Designer requires the following procedure. 
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.printButton = new System.Windows.Forms.Button();

        this.ClientSize = new System.Drawing.Size(504, 381);
        this.Text = "Print Example";

        printButton.ImageAlign =
           System.Drawing.ContentAlignment.MiddleLeft;
        printButton.Location = new System.Drawing.Point(32, 110);
        printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        printButton.TabIndex = 0;
        printButton.Text = "Print the file.";
        printButton.Size = new System.Drawing.Size(136, 40);
        printButton.Click += new System.EventHandler(printButton_Click);

        this.Controls.Add(printButton);
    }

}
}
Nathan
  • 819
  • 3
  • 9
  • 18
  • 1
    Please view it http://stackoverflow.com/questions/15985909/show-print-dialog-before-printing You need also create PrintDialog. – progpow Oct 11 '13 at 14:57
  • 1
    @StanislavAgeyev: I guess this fits better: http://stackoverflow.com/questions/5164323/how-to-set-to-default-printer – Daniel Abou Chleih Oct 11 '13 at 14:59
  • I added the print dialogue, now it shows, and then the fax settings show. – Nathan Oct 11 '13 at 15:01
  • Can you see any printer at dialog? – progpow Oct 11 '13 at 15:02
  • Yes, I see all my printers. When I select the printer, the fax settings still show up, Even after I set my default printer like in your link. – Nathan Oct 11 '13 at 15:03
  • Nevermind... I'm an idiot, I didn't notice the (FAX) at the beginning. Thanks! – Nathan Oct 11 '13 at 15:04
  • 1
    Just FYI your program should probably not make any assumptions about the type of printer used. The user might have a software printer to print to a file, or print to a program, or print to a network fax or anything they want. Almost every print operation should involve showing the PrintDialog to the user. – Trevor Elliott Oct 11 '13 at 15:25

2 Answers2

3

It's seems like a fax machine is your default printer, the easiest way to remedy this would be to add a print dialog before printing the page

PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
//Show Print Dialog
if (printDialog.ShowDialog() == DialogResult.OK)
{
//Print the page
printDocument.Print();
}

This will let the user select their desired printer before printing

Mitch B09
  • 33
  • 1
  • 7
3

Firs you should declare an object of System.Drawing.Printing.PrintDocument:

private System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();

Then add the code described in the previous answer:

PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
//Show Print Dialog
if (printDialog.ShowDialog() == DialogResult.OK)
{
//Print the page
printDocument.Print();
}
admiri
  • 314
  • 6
  • 14