Print Any document file like doc,docx ,jpg ,pdf using C# code without open the these document file doc,docx,jpg,pdf

(1)Put one text box control and two button on button for browse and another one for Print.



(2)Place this code in .cs file of this window form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;//this name space included for this task

namespace PrintWordFileInCsharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
//Below code print your word and any other document without open.
            ProcessStartInfo info = new ProcessStartInfo(textBox1.Text); //in this pass the file path
            info.Verb = "Print";
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(info);
        }
}
}

Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

9 comments:

  1. Hi Priti,
    Thanks for the article, Very informative. Aspose.Pdf for .NET C#is also another good option can be considered for Printing from a Windows Service. Here the code sample: The following code snippet shows how to print a PDF file

    //Create PdfViewer object
    PdfViewer viewer = new PdfViewer();
    //Open input PDF file
    viewer.BindPdf(@"c:\input.pdf");
    //Print PDF document
    viewer.PrintDocument();
    //Close PDF file
    viewer.Close();

    ReplyDelete
  2. you can try convert pdf to jpg in c#: http://www.xspdf.com/guide/pdf-jpg-converting/

    ReplyDelete
    Replies
    1. This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Dot Net Training in Chennai. or learn thru ASP.NET Online Training . Nowadays Dot Net has tons of job opportunities on various vertical industry.

      Delete
  3. Hi Priti,
    Thanks for this artical. I am using your code and it is printing pdf file. The only problem is that while printing pdf it opens file also.

    Thanks

    ReplyDelete
  4. Hi, i need to print .prn file
    Is this possible with this code?

    ReplyDelete
  5. Please check here.https://forums.asp.net/t/1884846.aspx?How+to+print+the+PRN+file+using+C+

    ReplyDelete