make printpreview of txt files

make a printpreview of a txt file, giving only the path of the file

Page 1 of 1

6 Replies - 5249 Views - Last Post: 12 September 2008 - 12:09 PM Rate Topic: -----

#1 Benetz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-September 08

make printpreview of txt files

Post icon  Posted 09 September 2008 - 07:40 AM

HELLO!! i'm trying to make an printpreview of a txt file, giving the path of that in a textBox. this is what i've done yet:

private void button1_Click(object sender, EventArgs e)
		{
			PrintDialog PrintDialog1 = new PrintDialog();
			System.Drawing.Printing.PrintDocument documentToPrint = new System.Drawing.Printing.PrintDocument();

			PrintDialog1.AllowSomePages = true;
			PrintDialog1.ShowHelp = true;
			PrintDialog1.Document = documentToPrint;

			DialogResult result = PrintDialog1.ShowDialog();
			printPreviewControl1 = printPreviewDialog1();

			if (result == DialogResult.OK)
			{
				documentToPrint.Print();
			}			

		}


how can i give the path of the file to the documentToPrint??

sorry for my english, i'm italian :D

Is This A Good Question/Topic? 0
  • +

Replies To: make printpreview of txt files

#2 djkitt   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Re: make printpreview of txt files

Posted 09 September 2008 - 10:26 AM

 // 0) add a string to hold the file
 private string fileText
.
.
.

// inside Button1_Click...

// 1) Open the file.
// 2) Read it into the fileText string.

// 3) Add an event handling routine (below) 
//4 ) Tie the event handling routine into to the documents PrintPage event.
this.documentToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);

// 5) Tie the PrintDocument into the PrintPreviewDialogue
 printPreviewDialog1.Document = documentToPrint;

// 6) Show the PrintPreviewDialogue
printPreviewDialog1.Show();
.
.
.



// PrintPage Event Handler

private void documentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            7) Draw the text from the file onto the graphics area exposed in the PrintPageEventArg 

            Graphics g = e.Graphics;
            g.DrawString(fileText, new Font("Arial", 12),Brushes.Black, new PointF(30,30));
        }




Note: If there is too much text to print on one page you will have to handle that in the PrintPage event. Look into the HasMorePages flag of the PrintPageEventArgs passed in vie 'e'...

Good Luck,

Kitt

Hope this helps.

This post has been edited by djkitt: 09 September 2008 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 Benetz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-September 08

Re: make printpreview of txt files

Posted 09 September 2008 - 11:57 PM

thank a lot... but one more question: how can i to this?

// 1) Open the file.
// 2) Read it into the fileText string.

i only have the path file in a textbox.......
Was This Post Helpful? 0
  • +
  • -

#4 djkitt   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Re: make printpreview of txt files

Posted 10 September 2008 - 07:19 AM

PsychoCoder wrote a nice little tutorial on reading the contents of a file into a string. Take a look at System.IO in C# Part 1.5

Later,


Kitt

This post has been edited by djkitt: 10 September 2008 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#5 Benetz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-September 08

Re: make printpreview of txt files

Posted 11 September 2008 - 12:14 AM

thanks, it's very good...but when i try it it doesn't work:

		public static string ReadEntireFileIfExists(string file)
		{
			//create a string variable to hold the contents of the file
			string fileContents = string.Empty;

			//always use a try...catch to deal 
			//with any exceptions that may occur
			try
			{

				//check to see if the file exists
				if (System.IO.File.Exists(file))
				{

					//create a new TextReader and open our file   

					using (TextReader reader = new StreamReader(file))   <--- Error		 // 1 impossible find the type or the name of  "textReader"'s space.
//second error: StreamReader.. the error is the same.

					{
						//now read the entire file at once into our variable
						fileContents = reader.ReadToEnd();
					}
					return fileContents;
				}
				else
				{
					throw new FileNotFoundException(file + " could not be found");

//third error: FileNotFoundExeption

				}
			}
			catch (Exception ex)
			{
				//handle your errors here
				Console.WriteLine(ex.Message);
				return fileContents;
			}
		}




:angry: why it doesn't work??
Was This Post Helpful? 0
  • +
  • -

#6 djkitt   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Re: make printpreview of txt files

Posted 12 September 2008 - 12:01 PM

Hey ther Benetz,

Sorry it took me a bit to respond. Anyway, I just popped the code in a little test program and it worked perfectly.

So, this is just a guess, as you did not put the code that calls this, but you may need to change your file string:


If you call the method with the following string it will not work even if the file exists
string filename = "C:\FileDirectory\MyCoolFile.txt";


This is because of the backslash characters [ \ ].
They are used in a string to specify that a special character is coming up...
So do either of the following:
Double up the backslashes - this indicates you do want a backslash:
filename =  "C:\\FileDirectory\\MyCoolFile.txt";


or, add an 'At' character [ @ ] before the first quote of the string - this indicates you don't want to use special characters:
filename = @"C:\FileDirectory\MyCoolFile.txt";



Like I said, this is just a guess but it happens often enough to make me go out on a limb here.

If this isn't the problem please post your calling code and I'll take a look.

Later,

Kitt

This post has been edited by djkitt: 12 September 2008 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 djkitt   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 192
  • Joined: 22-May 08

Re: make printpreview of txt files

Posted 12 September 2008 - 12:09 PM

OK, I think you should just ignore the above post.

After rereading your post I think maybe you just need to add:

using System.IO;



to your code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1