Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey. i hope all will be fine.
actually what i want to do is that, i want to select a file using file dialog facility, and then print it using printdocument.print() function.
now print() does not take any parameter. how i can tell the printer to which document to print??
and i want to know, what actually happens behind the print() method.
we are not giving any file path then what is going to print after hitting OK button.
plz help me to get print(file_path+name) functionality.
here is my effort !!!
C#
using System;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;


namespace InstalledAndDefaultPrinters
{
class Program
{


static void Main(string[] args)
{
   string filename="";
   foreach (string printer in PrinterSettings.InstalledPrinters)
       Console.WriteLine(printer);
   var printerSettings = new PrinterSettings();
   Console.WriteLine(string.Format("The default printer is: {0}", printerSettings.PrinterName));

   Console.WriteLine(printerSettings.PrintFileName);
   OpenFileDialog fdlg = new OpenFileDialog();
   fdlg.Title = "Open File Dialog";
   fdlg.InitialDirectory = @"C:\ ";
   fdlg.RestoreDirectory = true;
   fdlg.ShowDialog();
   Console.WriteLine(fdlg.Title);
   if (fdlg.ShowDialog() == DialogResult.OK)
   {
       filename = String.Copy(fdlg.FileName);
   }
   Console.WriteLine(filename);

   PrintDialog printdg = new PrintDialog();
   PrintDocument pd_doc = new PrintDocument();
   printdg.ShowDialog();
   if (printdg.ShowDialog() == DialogResult.OK)
   pd_doc.Print();
   }
   }
   {
Posted
Updated 21-Apr-19 22:39pm

Try setting your document as the Document property on your PrintDialog instance.

PrintDialog printdg = new PrintDialog();
PrintDocument pd_doc = new PrintDocument();
printdg.Document = pd_doc;
...


You obviously also have to make sure your PrintDocument represents the document you want to print.

Hope this helps,
Fredrik
 
Share this answer
 
Please have a look into the below MSDN article.. In addition to your existing code to print a file you need to have a StreamReader instance.

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900