All notes

Print PDF

Use default PDF app

SO: send a file document to the printer.

This opens the default printing app (e.g. Acrobat Reader) and tells it to send the PDF to the default printer, and then shuts down the app after three seconds.

The sample code opens the PDF in the application current registered to print PDFs, which is the Adobe Acrobat Reader on most people's machines. However, it is possible that they use a different PDF viewer such as Foxit.

Invocation of an interactive application (e.g. Adobe Acrobat) may not work if the user account your code runs under is not interactive. In that case, a command-line tool is your best bet.

private void SendToPrinter()
   ProcessStartInfo info = new ProcessStartInfo();
   info.Verb = "print";
   info.FileName = @"c:\output.pdf";
   info.CreateNoWindow = true;
   info.WindowStyle = ProcessWindowStyle.Hidden;

   Process p = new Process();
   p.StartInfo = info;

   if (false == p.CloseMainWindow())

There is a command line: print /D:printerName file1.pdf file2.pdf file3.pdf.

Use GhostScript

gswin32c -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile="\\spool\\\Server_Name\Printer_name" "C:\test.pdf"

SO: print PDF on default network printer using GhostScript.

/// <summary>
/// Prints the PDF.
/// </summary>
/// <param name="ghostScriptPath">The ghost script path. Eg "C:\Program Files\gs\gs8.71\bin\gswin32c.exe"</param>
/// <param name="numberOfCopies">The number of copies.</param>
/// <param name="printerName">Name of the printer. Eg \\server_name\printer_name</param>
/// <param name="pdfFileName">Name of the PDF file.</param>
/// <returns></returns>
public bool PrintPDF (string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName)
    ProcessStartInfo startInfo  = new ProcessStartInfo();
    startInfo.Arguments         = " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\" + printerName + "\" \"" + pdfFileName + "\" ";
    startInfo.FileName          = ghostScriptPath; 
    startInfo.UseShellExecute = false;

    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardOutput = true;

    Process process = Process.Start(startInfo);

    Console.WriteLine( process.StandardError.ReadToEnd() + process.StandardOutput.ReadToEnd() );

    if (process.HasExited == false) process.Kill();

    return process.ExitCode == 0;

There is a .Net wrapper of GhostScript library. You can use the wrapper directly.

Send Raw data

wcfNote: now only PS as input files work.

MicroSoft Support: How to send raw data to a printer.

public class RawPrinterHelper
    // Structure and API declarions:
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public class DOCINFOA
        [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
    [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level,  [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );

    // SendBytesToPrinter()
    // When the function is given a printer name and an unmanaged array
    // of bytes, the function sends those bytes to the print queue.
    // Returns true on success, false on failure.
    public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount)
        Int32    dwError = 0, dwWritten = 0;
        IntPtr    hPrinter = new IntPtr(0);
        DOCINFOA    di = new DOCINFOA();
        bool    bSuccess = false; // Assume failure unless you specifically succeed.

        di.pDocName = "My C#.NET RAW Document";
        di.pDataType = "RAW";

        // Open the printer.
        if( OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero ) )
            // Start a document.
            if( StartDocPrinter(hPrinter, 1, di) )
                // Start a page.
                if( StartPagePrinter(hPrinter) )
                    // Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
        // If you did not succeed, GetLastError may give more information
        // about why not.
        if( bSuccess == false )
            dwError = Marshal.GetLastWin32Error();
        return bSuccess;

    public static bool SendFileToPrinter( string szPrinterName, string szFileName )
        // Open the file.
        FileStream fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        BinaryReader br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        Byte []bytes = new Byte[fs.Length];
        bool bSuccess = false;
        // Your unmanaged pointer.
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength;

        nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        bytes = br.ReadBytes( nLength );
        // Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
        // Free the unmanaged memory that you allocated earlier.
        return bSuccess;
    public static bool SendStringToPrinter( string szPrinterName, string szString )
        IntPtr pBytes;
        Int32 dwCount;
        // How many characters are in the string?
        dwCount = szString.Length;
        // Assume that the printer is expecting ANSI text, and then convert
        // the string to ANSI text.
        pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        // Send the converted ANSI string to the printer.
        SendBytesToPrinter(szPrinterName, pBytes, dwCount);
        return true;

Print to specified printer

Method 1. codeProject: print a word using word automation.

//Set Printer 
object wb = app.WordBasic;

//first arg is a printer name
object[] argValues = new object[] {p.PrinterSettings.PrinterName, 1}; 
String [] argNames = new String [] {"Printer","DoNotSetAsSysDefault"};


Method 2. To send a printout directly to a specific printer, simply set the PrintDocument object’s PrinterSettings.PrinterName property to the printer’s name.

// Print.
private void btnPrint_Click(object sender, EventArgs e)
    // Select the printer.
    pdocSmiley.PrinterSettings.PrinterName =
        "HP Deskjet F300 Series";
    // Print.