Processing event of GhostscriptProcessor class

Dec 13, 2013 at 12:55 PM
I am having difficulty subscribing to the Processing event of GhostscriptProcessor class. Any help would be greatly appreciated. Here's my code.

Public Class PostscriptToPDFConverter
Inherits Ghostscript.NET.Processor.GhostscriptProcessor

Public Event UpdateExtraInfo(ByRef lstrInfo As String, ByVal lblnLog As Boolean)

Public Sub Convert(lstrInputPSFile As String)

    Dim lstrOutputPDF As String = System.IO.Path.ChangeExtension(lstrInputPSFile, ".pdf")
    Dim lstrInfo As String = String.Empty

    lstrInfo = String.Format("Convert {0} to {1}", System.IO.Path.GetFileName(lstrInputPSFile), System.IO.Path.GetFileName(lstrOutputPDF))
    RaiseEvent UpdateExtraInfo(lstrInfo, True)

    Dim switches As New List(Of String)()
    switches.Add("-empty")
    switches.Add("-dQUIET")
    switches.Add("-dSAFER")
    switches.Add("-dBATCH")
    switches.Add("-dNOPAUSE")
    switches.Add("-dNOPROMPT")
    switches.Add("-sDEVICE=pdfwrite")
    switches.Add(String.Format("-sOutputFile={0}", lstrOutputPDF))
    switches.Add("-f")
    switches.Add(lstrInputPSFile)

    AddHandler MyBase.Processing, AddressOf GSProcessing
    MyBase.StartProcessing(switches.ToArray(), Nothing)
    RemoveHandler MyBase.Processing, AddressOf GSProcessing

End Sub

Private Sub GSProcessing(sender As Object, e As Ghostscript.NET.Processor.GhostscriptProcessorProcessingEventArgs)
    Dim lstrInfo As String = String.Format("Page: {0} out of {1}", e.CurrentPage, e.TotalPages)
    RaiseEvent UpdateExtraInfo(lstrInfo, True)
End Sub
End Class
Coordinator
Dec 13, 2013 at 1:07 PM
Edited Dec 13, 2013 at 1:15 PM
Hi,

Remove "switches.Add("-dQUIET")" as this switch suppresses routine information comments on standard output which Processing event relays on.

Cheers,
Josip
Dec 13, 2013 at 1:29 PM
Thanks for the prompt response, but it hasn't made any difference. I also tried commenting out all the switches below to see if they had any effect but still no event:-
' switches.Add("-dQUIET")
' switches.Add("-dSAFER")
' switches.Add("-dBATCH")
' switches.Add("-dNOPAUSE")
' switches.Add("-dNOPROMPT")
Coordinator
Dec 14, 2013 at 10:59 AM
Hi,

I did not saw earlier that you are converting PostScript to PDF. I spoke with the developers of the Ghostscript native library and they said that the thing is that a PDF file is a structured format which includes (amongst other things) a definition of the number of pages. PostScript is a streamable simple format and does not contain that information. Some PostScript files contain 'DSC' comments which declare the number of pages in the document. However these can include the not terribly useful 'atend' value, which means that the number of pages is defined at the end of the program. By which time, of course, an interpreter knows how many pages there are, because its processed them all.

That means that by default for the PostScript files there is no way to get back information from the Ghostscript which PostScript page is processing.

Cheers,
Josip
Dec 17, 2013 at 10:38 AM
Makes sense. However couldn't it still trigger the event and just populate the e.CurrentPage property whilst leaving the e.TotalPages property at zero or minus one or something, just so we can show some on-screen activity during processing?
Jan 29, 2014 at 10:57 AM
Any news on this one?
Coordinator
Jan 29, 2014 at 4:23 PM
Edited Jan 29, 2014 at 4:24 PM
This could be eventually done by manually parsing the postscript file, determining page start/end and sending each page to render separately. Something what I did in the [GhostscriptViewerPsFormatHandler.cs] which relies on the DSC comments. (https://ghostscriptnet.codeplex.com/SourceControl/latest#Ghostscript.NET/Ghostscript.NET/Viewer/FormatHandlers/GhostscriptViewerPsFormatHandler.cs).

When I find some time I will try to put together some code...