0

I create prn code from pdfs and send them to printers in a C# code to automatize printing jobs. To do that, I use ghost script with following parameters.

gswin32c -dNOPAUSE -dBATCH -sDEVICE=laserjet -sOutputFile="c:/temp/out.prn" "NumberedPages.pdf"

enter image description here

This commandline arguments generates a prn file named out.prn. When I send this file to the printer with following C# code, it prints the pdf file successfully.

 public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            uint returnedValue = 0;
            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.
                returnedValue = StartDocPrinter(hPrinter, 1, di);

                if (0 != returnedValue)
                {
                    // Start a page.
                    if (StartPagePrinter(hPrinter))
                    {
                        // Write your bytes.
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            // If you did not succeed, GetLastError may give more information
            // about why not.
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return bSuccess;
        }

(https://stackoverflow.com/a/29564132/3079364)

I also want to monitor the print jobs if they are successfully printed by the printer or not. But the prn file which is generated by ghost script does not reflects the correct number of pages. See below

Printed by ghostscript

Captured from CZ Print job tracker

When I print this document using Word or Adobe, CZ Print job tracker shows the correct number of pages.

When printed by Word

Is there any parameter that I can add to ghost script command to correct this information ?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
user3079364
  • 171
  • 1
  • 19

1 Answers1

2

The output file is simply a PCL file, because that's what laserjet devices understand. This does not contain any information about the number of pages in the file.

The reason the print spooler thinks there's only one page is because you haven't told it differently. You have opened a file, started a page, and spewed the content directly to the printer, there is no way for the spooler to know how many pages that stream contains if you don't tell it where each one starts and stops.

You call StartPagePrinter once, so the print spooler (not unreasonably) assumes there is only one page. If you call StartPagePrinter and EndPagePrinter once for each page, then you will get the correct number of pages. Of course, that means knowing where each page begins and ends in the file output by Ghostscript, which you don't know.

Your best bet would be to use the %d format to OutputFile to produce one file per page, then the page counting will be correct.

There doesn't seem to be any way to tell the print spooler how many pages there are when you are sending data directly to the printer. I guess that's not surprising, since the print spooler isn't actually involved.

KenS
  • 30,202
  • 3
  • 34
  • 51