Search code examples
c#pdfprintingghostscriptpostscript

Print PDF using GhostScript


I am in need of your support on the following issue since its pulling me for a while. We have a small c# utility, which print given PDF using GhostScript. This print as expected but fail to retain the page formatting’s. However, pages are printed as expected when I switch Adobe Acrobat in place of GhostScript. So I presume, I am making some obvious mistake on the GhostScript's command line arguments .

Background

Following is the core c# logic, which print a given PDF file with varying style across each pages. The given PDF file has pages;

  1. with inconsistent font style and colour
  2. some of the pages have normal font size where others are printed in extra small
  3. some of the pages has recommended margin but others have very small margin
  4. some of the pages are in colour and the rest in grey.
  5. some of the pages are landscape in style where other are portrait

In concise, the PDF which I am trying to print is nothing but a consolidation (joining individual pdfs into one large pdf) of numerous small sized pdf document with varying fonts style, size, margins.

Issue

Following logic use GhostScript(v9.02) to print PDF file. Though the following logic print any given PDF, it fail to retain the page formatting including header, footer, font size, margin, orientation ( my pdf file has pages those both landscape and portrait).

Interestingly, if I use acrobat reader to print the same PDF then it will print as expected along with all page level formatting's.

PDF specimen: First section, Second section

  void PrintDocument()
    {
         var psInfo = new ProcessStartInfo();
                psInfo.Arguments =
                    String.Format(
                        " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\{0}\" \"{1}\"",
                        GetDefaultPrinter(), @"C:\PDFOutput\test.pdf");
                psInfo.FileName = @"C:\Program Files\gs\gs9.10\bin\gswin64c.exe";
                psInfo.UseShellExecute = false;

        using (var process= Process.Start(psInfo))
        {
            process.WaitForExit();
        }
    }

Solution

  • Answer - UPDATE 16/12/2013

    I managed to get it fixed and wanted to share the working solution if it help. Special thanks to 'KenS' since he spend a lot of time to guide me.

    To summarise, I decided to use GSView along with GhostScript to print PDF to bypass Adobe. The core logic is given below;

     //PrintParamter is a custom data structure to capture file related info
    private void PrintDocument(PrintParamter fs, string printerName = null)
            {
                if (!File.Exists(fs.FullyQualifiedName)) return;
    
                var filename = fs.FullyQualifiedName ?? string.Empty;
                printerName = printerName ?? GetDefaultPrinter(); //get your printer here
                
                var processArgs = string.Format("-dAutoRotatePages=/All -dNOPAUSE -dBATCH -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dEmbedAllFonts=true -dSubsetFonts=true -dPDFSETTINGS=/prepress -dNOPLATFONTS -sFONTPATH=\"C:\\Program Files\\gs\\gs9.10\\fonts\" -noquery -dNumCopies=1 -all -colour -printer \"{0}\" \"{1}\"", printerName, filename);
                try
                {
    
                    var gsProcessInfo = new ProcessStartInfo
                                            {
                                                WindowStyle = ProcessWindowStyle.Hidden,
                                                FileName = gsViewEXEInstallationLocation,
                                                Arguments = processArgs
                                            };
                    using (var gsProcess = Process.Start(gsProcessInfo))
                    {
                       
                        gsProcess.WaitForExit();
                       
                    }
                
            }