Ghostscript.NET.dll print pdf to specified printer

Raju Padhara picture Raju Padhara · Feb 3, 2015 · Viewed 10.4k times · Source

How to print pdf using ghostscript api. I tried google but still not getting proper solution. Please help me how i do this task.

Answer

HABJAN picture HABJAN · Feb 3, 2015

This should work for you (by using Ghostscript.NET wrapper):

using System;
using System.Collections.Generic;
using Ghostscript.NET.Processor;

namespace Ghostscript.NET.Samples
{
    public class SendToPrinterSample : ISample
    {
        public void Start()
        {
            // YOU NEED TO HAVE ADMINISTRATOR RIGHTS TO RUN THIS CODE

            string printerName = "YourPrinterName";
            string inputFile = @"E:\__test_data\test.pdf";

            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                List<string> switches = new List<string>();
                switches.Add("-empty");
                switches.Add("-dPrinted");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOSAFER");
                switches.Add("-dNumCopies=1");
                switches.Add("-sDEVICE=mswinpr2");
                switches.Add("-sOutputFile=%printer%" + printerName);
                switches.Add("-f");
                switches.Add(inputFile);

                processor.StartProcessing(switches.ToArray(), null);
            }
        }
    }
}