Print PDF using GhostScript

I need your support on the following question, as it has been pulling me for a while. We have a small utility c#that prints using PDFwith GhostScript. This prints as expected, but does not preserve the page format. However, the pages print as expected when I switch Adobe Acrobatinstead GhostScript. Therefore, I assume that I am making an obvious mistake in the GhostScript command line arguments.

Background

Below is the basic C # logic that prints a given PDF file with a different style on each page. There are pages in this PDF file;

  • with inconsistent font style and color
  • some pages have a normal font size, where others print in extra small
  • some of the pages recommended margin, but others have a very small margin.
  • some pages are in color and others are gray.
  • some of the pages have a landscape in style, while others have a portrait.

In a compressed form, the PDF that I am trying to print is nothing more than consolidation (combining individual PDF files into one large pdf) from a large small PDF format with various font types, sizes, margins.

Question

Following the logic, use GhostScript(v9.02)to print a PDF file. Although the following logic prints any given PDF file, it cannot preserve page formatting, including header, footer, font size, margin, orientation (there are pages in my pdf that are both landscape and portrait).

, Acrobat Reader PDF , , , .

PDF-: ,

  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();
        }
    }
+4
3

- 16/12/2013

, . "KenS", , .

, , , GSView GhostScript PDF, Adobe. :

 //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();

                }

        }
+2

, , , GSView, Ghostscript.

, GSView Ghostscript , , .

PDF, , , , , Ghostscript. (, ), , , , , .

Ghostscript .

, , (, -dPDFSETTINGS), PDF, - (, ).

, , , , ( Ghostscript), . , , , , , .

+2

GSPRINT.

, gsprint.exe/gswin64c.exe/gsdll64.dll .

:

    // This uses gsprint (mind the paths)
    private const string gsPrintExecutable = @"C:\gs\gsprint.exe";
    private const string gsExecutable = @"C:\gs\gswin64c.exe";

    string pdfPath = @"C:\myShinyPDF.PDF"
    string printerName = "MY PRINTER";


    string processArgs = string.Format("-ghostscript \"{0}\" -copies=1 -all -printer \"{1}\" \"{2}\"", gsExecutable, printerName, pdfPath );

            var gsProcessInfo = new ProcessStartInfo
                                    {
                                        WindowStyle = ProcessWindowStyle.Hidden,
                                        FileName = gsPrintExecutable ,
                                        Arguments = processArgs
                                    };
            using (var gsProcess = Process.Start(gsProcessInfo))
            {

                gsProcess.WaitForExit();

            }
+2

All Articles