Hello,
I'm having newby difficulties printing from a richtextbox to a printer.
I have built a small application (Like part of notepad) with file load and print. I can load file or write to the textbox, but when printing (using the PrintDialog to select and set the printer) I always get an empty page from the default printer.
Can someone show me how to make this printing and print previw work? (See attched code). Why do I always get my default printer, even when selecting another network printer?
Sam
C#
using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
namespacePrintTextBox
{
publicpartialclassForm1 : Form
{
internalstring MyFileName;
internalbool newfile;
internalbool open;
publicForm1()
{
InitializeComponent();
this.Show();
}
privatevoidrichTextBox1_TextChanged(object sender, EventArgs e)
{
}
privatevoidnewToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Clear();
newfile = true;
Text = "PrintTextBox";
}
privatevoidopentoolStripMenu_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.DefaultExt = "Text files |*.txt";
dlgOpen.Filter = "Text files |*.txt|RTF Files (*.rtf)|*.rtf|All files |*.*";
dlgOpen.Multiselect = true;
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
this.MyFileName = Text = dlgOpen.FileName; //Display and remember the open file name for saving
StreamReader streamReader = new StreamReader(dlgOpen.FileName);
this.richTextBox1.Text = streamReader.ReadToEnd();
streamReader.Close();
open = true; newfile = false;
}
}
catch (ArgumentException) { }
catch (IOException) { }
catch (FormatException) { }
finally { }
}
privatevoidPrintoolStripMenut_Click(object sender, EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog();
if (dlgPrint.ShowDialog() == DialogResult.OK)
{
//Print a document or print the textbox
PrintDocument printdoc = new PrintDocument();
printdoc.DocumentName = "MyDocument.txt";
printdoc.Print(); //Prints an empty page!!!!//// or print the richTextBox ????//
}
}
privatevoidPreviewtoolStripMenu_Click(object sender, EventArgs e)
{
PrintPreviewDialog dlgPreview = new PrintPreviewDialog();
if (dlgPreview.ShowDialog() == DialogResult.OK)
{
//same problem as print.
}
}
privatevoidExittoolStripMenu_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Hey sam,
The DocumentName property of PrintDocument class is used to display name in printer queue, its not the actual file path.
use following code insted:
naura paxRank #2,843 · 502 reputationPosted Oct 25, 2009, 10:25 PM EDT
The DocumentName property of PrintDocument class is used to display name in printer queue, its not the actual file path.
use following code insted:
private void printButton_Click(object sender, EventArgs e) { try { streamToPrint = new StreamReader ("C:\\My Documents\\MyDocument.doc"); try { printFont = new Font("Arial", 10); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage); pd.Print(); } finally { streamToPrint.Close(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } }Please mark accepted if useful
bebo.