Print any Document File using a C# Code without Opening Documents

If you want to print document such as the doc, docx, pdf, jpeg etc. with C# code. Follow this Post.

Here I have to use the filedialog control of dot net framework for the file browsing purpose and add this namespace using System.Diagnostics and using ProcessStartInfo class i have to call the particular document file and print this file.

Using ProcessStartinfo Class we can call the external resources which is outside of dot net technology. Window based Application:=>Add opendialog control on Designer Form.

d

Add this code in Form.cs in : Add this code on the Browse Button Click For browsing file.

private void btnBrowse_Click(object sender, EventArgs e)

{

   DialogResult dr = openFileDialog1.ShowDialog();

   string[] s = openFileDialog1.FileName.Split('.');

   if (dr.ToString() == "OK")

   {

        if (s.Length > 1)

        if (s[1] == "doc" || s[1] == "docx" || s[1] == "jpg")

        txtFileName.Text = openFileDialog1.FileName;

    else

     MessageBox.Show("Please select doc,docx,jpeg file !!");

 }

}

 
Add This code on print Button Click:=>Here i have to use processStartInfo dot net inbuilt class for accessing external resourcess like do,docx etc file.

private void btnPrint_Click(object sender, EventArgs e)

{

     if (string.IsNullOrEmpty(txtFileName.Text.Trim()))
     {

         txtFileName.BackColor = Color.Yellow;

         MessageBox.Show("Please Select file.");

          return;
     }

    //Using below code we can print any document

    ProcessStartInfo info = new ProcessStartInfo(txtFileName.Text.Trim());

    info.Verb = "Print";

    info.CreateNoWindow = true;

    info.WindowStyle = ProcessWindowStyle.Hidden;

    Process.Start(info);
}

Next Recommended Reading Convert Text Document to PDF File