Click here to Skip to main content
Click here to Skip to main content

Silent Print Using C#

, 14 Mar 2012
Rate this:
Please Sign up or sign in to vote.
Print you PDF silently using C#

Introduction

Most developers when they try to send jobs to a printer, they use Acrobat Reader via command line by setting the required parameters.

The problem is, that leaves Acrobat open if your client won't accept the pop-up window.

In this article I'll show you how to avoid this issue by using two useful utilities.

Background

In this solution, we'll use the suite of software Ghost Script developed by Artifex and Ghostgum (a graphical interface for GhostScript). Ghostscript is an interpreter for the PostScript page description language used by laser printers. In contrast, GSview allows you to preview and print your PDFs and other documents (.ps, .epi, and eps).

Using the code

To accomplish our task, we need to send the required paramaters to gsprint.exe under /GSView.

Note

GSview requires Ghostscript. GSview is available for Windows, Linux, and OS/2.

private void Form1_Load(object sender, EventArgs e)
{
    ManagementObjectSearcher searcher;
    string Win32_Printer = "SELECT * FROM Win32_Printer";
    searcher = new ManagementObjectSearcher(Win32_Printer);
    foreach (ManagementObject mgmtobj in searcher.Get())
    {
        comboBoxPrinterName.Items.Add(mgmtobj["Name"].ToString()); return;
    }
}

private void buttonPrint_Click(object sender, EventArgs e)
{
    PrinterThread(comboBoxPrinterName.Text, textBoxDocumentPath.Text, true);
}

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    textBoxDocumentPath.Text = openFileDialog1.FileName;
}

private void textBoxDocumentPath_MouseDoubleClick(object sender, MouseEventArgs e)
{
    openFileDialog1.ShowDialog();
}

public void PrinterThread(string printerName, string fileName, bool portrait)
{
     string gsArguments, gsLocation;
     ProcessStartInfo gsProcessInfo;
     Process gsProcess;
 
     if (portrait)
     {
         gsArguments = string.Format("-noquery -portrait -printer \"{0}\" \"{1}\"",
             printerName, fileName);
         gsLocation = @"C:\Program Files\Ghostgum\gsview\gsprint.exe";
     }
     else
     {
         gsArguments = string.Format("-noquery -landscape -printer \"{0}\" \"{1}\"",
              printerName, fileName);
         gsLocation = @"C:\Program Files\Ghostgum\gsview\gsprint.exe";
     }
     gsProcessInfo = new ProcessStartInfo();
     gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
     gsProcessInfo.FileName = gsLocation;
     gsProcessInfo.Arguments = gsArguments;
     gsProcess = Process.Start(gsProcessInfo);
     gsProcess.WaitForExit();
}

Conclusion

Many third party libraries still use Adobe Reader so here you get to do a clean job.

License

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

About the Author

R.Tarik
Web Developer
Morocco Morocco
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 14 Mar 2012
Article Copyright 2012 by R.Tarik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid