0

This is my silent PDF print code for C# and Ghostscript.NET library:

        public bool Print(string inputFile, string printerName, int nrcopies)
        {
            if (nrcopies < 1)
                nrcopies = 1;
            if (!File.Exists(inputFile) || !inputFile.ToLower().EndsWith(".pdf"))
                throw new ApplicationException("File not found or not valid");
            bool defaultPrinter = String.IsNullOrWhiteSpace(printerName);
            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=" + nrcopies);
                switches.Add("-sDEVICE=mswinpr2");
                if(defaultPrinter)
                    switches.Add("-dQueryUser=3");
                else
                    switches.Add("-sOutputFile=%printer%" + printerName);
                switches.Add("-f");
                switches.Add(inputFile);
                try
                {
                    processor.StartProcessing(switches.ToArray(), null);
                }
                catch (Exception) { }
            }
            return true;
        }

I want to collage pages in case of multiple-copies multiple-page print. I tried to print to many devices, for example, also in windows's PDF printer and I get always not-collated print.

This is what I want:

enter image description here

But this is what I get using this param:

switches.Add("-dNumCopies=" + nrcopies);

enter image description here

This is the equivalent GS command:

gswin64.exe -empty -dPrinted -dNOSAFER -dNumCopies=2 -sDEVICE=mswinpr2 -dQueryUser=3 -f "C:\Users\myuser\Desktop\TEST.pdf"
Tobia
  • 9,165
  • 28
  • 114
  • 219
  • Ghostscript won't do collation for you, so if you want pages collated it is only possible if the **printer** supports it. Which a PDF printer I strongly suspect will not. I can't help at all with Ghostscript.NET, if you give a command line (including a standard Windows printer, such as the generic PostScript printer) then I can look at it. – KenS Jun 22 '18 at 16:51
  • My physical printer supports collation, I'm sure and I tried to set it as default option. I will try with a command line, but can you tell me where can I find docs about - dNumCopies argument? I was really surprised about not collated page print when I set dNumCopies more then one. – Tobia Jun 22 '18 at 17:21
  • NumCopies just has the same effect as setting NumCopies in the page device dictionary. Ghostscript doesn't do collation, at all. The documentation is here: https://www.ghostscript.com/doc/9.23/Language.htm#Device_parameters – KenS Jun 22 '18 at 17:26
  • I will give you an update when I come back to office, for now my bad solution is to repeat n times a single copy print. – Tobia Jun 22 '18 at 17:33
  • I'm on the road as well, will be back in my office on Tuesday – KenS Jun 22 '18 at 19:57
  • I added the GS command, the result is the same: for -dNumCopies>1 my printer (Sharp driver) doesn't collate, but if I print from any other sw link MS Word, it collates as expected. – Tobia Jun 25 '18 at 06:30
  • Well, actually you've added your Ghostscript.NET code, not a Ghostscript command line as such. I can't help you with Ghostscript.NET. I notice you are trying to 'silently print'. If I were you I would not try that until I had collation working. Try getting the print dialog up and look at it to see how collation is set. The mswinpr2 device (which isn't actually intended as a silent print server, and is not really sophisticated enough for the job) uses the 'default' configuration, which may not be what you think it is, when printing without user input. – KenS Jun 25 '18 at 07:13
  • @KenS please look at the end of my question, I added the command line for gswin64.exe, this one is not silent. How can I collect information from that command line, it outputs only few warning about not finded fonts. – Tobia Jun 25 '18 at 07:32
  • You've still set QueryUser=3 which selects the default printer sand proceeds without user interaction. Don't do that. Set it to 1 or 2 and when the print dialog pops up, look at the way the printer is setup, does it have collating set ? Setting it to 3 uses the default printer **and its default settings** I wouldn't be surprised to find the default printers settings do not set collate. – KenS Jun 25 '18 at 20:53
  • The other option, of course, is to debug the code and see what's in the printer structure. By the way, I think you should review the Ghostscript licence (AGPL v3) to ensure you are not violating the terms. – KenS Jun 26 '18 at 08:16
  • I looked at default settings and coolation is set. However -dNumCopies=2 param it doesn't collate but I found every page printed twice. I already looked at AGPL and in my opinion it is suitable for my application. – Tobia Jun 26 '18 at 12:33
  • Rather than look at the default settings, look at the printer dialog when Ghostscript executes. I've seen cases before when the 'default settings' are not the ones set when Ghostscript gets the information back from Windows. If that seems OK, then its time to break out the debugger and look at the contents of the DEVMODE structure returned by Windows, the dmCollate member, and see what its set to. You want to look in gdevwpr2.c, about line 1079, in the function win_pr2_getdc(). Check that dmFields has the DM_COLLATE bit set, then check dmCollate to see if its DMCOLLATE_TRUE. – KenS Jun 26 '18 at 12:53
  • I had to remove "-dQueryUser=3" to see the print dialog. I found that printer dialog doesn't have the copies number set. My command has -dNumCopies=2 but the print dialog has 1 as copies number. In the printer properties I found the collate set, but I think that it may be ignored if the number of copies is 1. After the first print job I got another printer dialog (for the second copy?). So -dNumCopies=2 produces two print dialog of nr 1 copy. If I force 2 copies in the print dialog the print is collated successfully. – Tobia Jun 28 '18 at 08:30
  • 1
    NumCopies is a directive to Ghostscript (actually its part of the PostScript language), it simply causes GS to produce 'NumCopies' of each page. That value does not get sent to the printer. So it looks like your printer only does collation if you tell **it** to do the extra copies, which makes some sense, otherwise how could it know how many pages there are. So you need to do this yourself; set the printer defaults up with the required number of copies, and set collate to true and don't tell Ghostscript to do copies for you (ie leave NumCopies alone). – KenS Jun 28 '18 at 11:54
  • Ok, thank you for your patience, maybe I have to move the problem outside Ghostscript topic. I have to interact with Windows printers (se copies number, be sure it uses the default printer configuration) and I have some problem using GS. Thank you so much KenS! – Tobia Jun 28 '18 at 12:17

1 Answers1

0

using a shell script like below helps Just add the requested postscript files as arguments...

TMPFILE=`mktemp` || exit 1
#echo $@
gs -dNOPAUSE -sDEVICE=ps2write -sOUTPUTFILE=$TMPFILE -dBATCH $@ 
echo "enter copies"
read copies
a=0
while [ $a -lt  $copies ]
do
  a=`expr $a + 1`
  lpr $TMPFILE

done
rm -f $TMPFILE
ole
  • 1