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/.

3 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