0

Print path is:

  1. Excel or VB.net PrintDocument >
  2. Postscript >
  3. Multiple PS files to single PDF using GhostScript >
  4. Add Stamp using iTextPDF >
  5. Create PCL file for specific printer from PDF using custom PJL commands for duplex/staple etc. >
  6. Send PCL file to printer using LPR

In step 3 is it possible to create a PDF (using GhostScript) from PS files while reducing the pages and having 4 to a page (4 up)? Or 2 to a page (2 up)?

4 up means 4 pages reduced to fit on one sheet of paper:

enter image description here

    'This uses a list of PS files to create one PDF
    Private Shared Sub ConvertToPDF(ByVal PSPathFileList As List(Of String), _
                             ByVal PDFPathName As String, _
                             ByVal WaitForExit As Boolean, ByVal DeletePS As Boolean)

        'check that all files exist
        PSPathFileList.ForEach(AddressOf CheckFiles)

        'check old pdf file
        If IO.File.Exists(PDFPathName) Then
            Throw New ApplicationException( _
                "PDF cannot be created. File already exists: " & PDFPathName)
        End If

        'convert engine
        Dim myProcInfo As New ProcessStartInfo
        myProcInfo.FileName = DanBSolutionsLocation & "Misc\GhostScript\GSWIN32C.EXE"
        Debug.Print(myProcInfo.FileName)

        'write file names to text file as the list can be very long
        Dim tempPath As String = IO.Path.GetDirectoryName(PSPathFileList.Item(0))
        Dim fiName2 As String = tempPath & IO.Path.GetFileNameWithoutExtension(PDFPathName) & ".txt"

        Dim ft As New StreamWriter(fiName2)
        ft.WriteLine("-sDEVICE=pdfwrite -q -dSAFER -dNOPAUSE -sOUTPUTFILE=""" & PDFPathName & """ -dBATCH ")
        For i As Long = 0 To PSPathFileList.Count - 1
            ft.WriteLine(Chr(34) & PSPathFileList.Item(i) & Chr(34))
        Next
        ft.Close()

        'set args to text file
        myProcInfo.Arguments = """@" & fiName2 & """"

        'set up for output and errors
        myProcInfo.UseShellExecute = False
        myProcInfo.RedirectStandardOutput = True
        myProcInfo.RedirectStandardError = True
        Debug.Print(myProcInfo.Arguments)

        'do the conversion
        Dim myProc As Process = Process.Start(myProcInfo)

        Debug.Print(myProc.StandardOutput.ReadToEnd)
        Debug.Print(myProc.StandardError.ReadToEnd)

        If WaitForExit Then
            'wait for finish; (no more than 60 seconds)
            myProc.WaitForExit(60000)

            'delete PS
            If DeletePS Then
                PSPathFileList.ForEach(AddressOf DeleteFiles)
            End If
        End If

    End Sub
D_Bester
  • 5,723
  • 5
  • 35
  • 77

1 Answers1

1

Yes its possible to take a PostScript file with 'N' pages and produce a single PDF file with 'M' pages, where each page has N/M of the original pages, shrunk and offset.

This is called imposition, there are are commercial tools to do this, or you can use psnup.

Alternatively you can write the PostScript to do this yourself (PostScript is a programming language after all). Probably the easiest way is a custom BeginPage routine.

This routine should examine the count of pages, and use that to determine which quadrant to draw the virtual page onto. That means altering the CTM to scale the content and translate the origin. While the scaling remains constant, the translation varies with each virtual page, in order to move it to a different quadrant. Oh and you will need a custom EndPage as well, so that only every 4th page is sent to the output.

A more sophisticated implementation would also override setpagedevice in order to set the scaling individually for each media request, in case the pages are not all on the same media.

Or you could distill the whole lot into one PDF file, and then use the program I wrote here to do the job in PDF.

Of course your multiple conversions, PS->PDF->PCL are quite likely to result in compromises, due to the different capabilities of each page description language. I hope the pages have quite simple content......

Community
  • 1
  • 1
KenS
  • 30,202
  • 3
  • 34
  • 51
  • Thanks! BeginPage/EndPage looks interesting but a lot of work. Same with your program to do 2up. I'll check out psnup. This answer is gold; thanks a million! – D_Bester Nov 09 '15 at 00:38
  • About PDF to PCL: Don't most printer drivers convert PDF to PCL anyway when I print from Adobe Reader? This PCL is what gets spooled and passed on to the printer? – D_Bester Nov 09 '15 at 00:38
  • Actually that bit about creating a PCL file may not be accurate, I'm not quite sure. I'm actually wrapping the PDF file with some PJL commands to control duplex and staple along with `@PJL ENTER LANGUAGE=PDF`. So I guess I'm not actually converting the PDF file at all. Code is at: http://stackoverflow.com/questions/33548008/how-would-i-print-a-pdf-to-pcl-file-while-reducing-the-pages-and-having-4-to-a-p – D_Bester Nov 09 '15 at 00:43