Thread: PrintDocument and PrintPreviewDialog

  1. #1
    Registered User
    Join Date
    Mar 2007

    PrintDocument and PrintPreviewDialog

    I am having issues getting my text to appear on a print preview document. I have not tried printing yet, since I cannot get the print preview to work correctly. I have looked through the MSDN references and found nothing related to setting the text, only setting the document. The document does exist and I have checked that multiple times. I think this is simply coming down to a very stupid error on my part and I cannot find out what it is.

            private void printPreviewToolStripButton_Click(object sender, EventArgs e)
                if (open)
                    PrintDocument pd = new PrintDocument();
                    PrintPreviewDialog ppd = new PrintPreviewDialog();
                    string docName = "";
                    string docPath = "";
                    /* do other stuff not related to printing */
                    docName = "pressureconversion.txt"; // the test file with valid text
                    docPath = "./";
                    pd.DocumentName = docPath + docName;
                    //MessageBox.Show(docName); // test to see correct name
                    //MessageBox.Show(docPath); // test to see correct path
                    ppd.Document = pd;
                    MessageBox.Show("There is nothing currently open!");

  2. #2
    Registered User
    Join Date
    Aug 2003
    PrintDocument doesnt work the way you think it does. It is not used to print a document residing on the harddrive automatically, you need to connect a handler to the PrintPage event and call Print for anything to happen. Here is more documentation on how to use PrintDocument and an example of printing a txt file:

    PrintDocument Class (System.Drawing.Printing)

    This page contains an example of using a PrintDocument together with a PrintPreviewDialog:
    PrintPreviewDialog Class (System.Windows.Forms)

    Merging these 2 should give you a satisfactory result.

Popular pages Recent additions subscribe to a feed