質問

I am trying to produce "c:\output.pdf" from existed and well formed "output.ps" file with VB.NET and shell to gswin32c.exe which is in current directory.

But I obviously can't write shell command properly:

If LCase(p_printer).Contains("ghostscript") Then

    ' to not show old one
    IO.File.Delete(OutputPDF)
    If IO.File.Exists(InputPS) Then
        Dim commandString As String = """gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=" & OutputPDF & " " & InputPS & """"
        Debug.Print(commandString)

        Shell(commandString, AppWinStyle.NormalFocus)
        If IO.File.Exists(OutputPDF) And bln_showpdf Then
            'show PDF
            Start(OutputPDF)
        End If
    Else
        MsgBox(InputPS + " do NOT exists.", MsgBoxStyle.Critical)
    End If
End If

From cmd window those command regularly produce "output.pdf"

What is incorrect and how to get it working?

役に立ちましたか?

解決

Dim InputPS as String = "C:\Temp\output.ps" 'must use 8.3 file naming convention
Dim OutputPDF as String = "C:\Temp\output.pdf" 'must use 8.3 file naming convention
Dim CommandString as String = "C:\GS\gswin32c.exe -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dQUIET -sOUTPUTFILE=" & OutputPDF & " " & InputPS

Debug.Print(CommandString)
Shell(CommandString, AppWinStyle.NormalFocus)

Actually the command string doesn't need quotation marks, I tested it without them. You must use 8.3 file naming convention though. Note that in this code the input and output filenames do not start or end with quotation marks; That is why you must use 8.3 file naming convention for this to succeed. And no spaces in the file names or paths.

Your problem is that it can't find the file; relying on the currently active directory is not a good practice as it can cause problems. The solution is to provide full path and file name with no spaces and using 8.3 file naming convention for both path and file name.

Also make sure that GSDLL32.DLL is in the same folder as GSWin32C.exe.

他のヒント

I did some more testing and found that by using quotation marks in the command string, it takes long file names just fine.

Public Function ConvertToPDF(ByVal svPsFileName, ByVal svPDFName)

    'check for file
    If Not IO.File.Exists(svPsFileName) Then
        Throw New ApplicationException(svPsFileName & " cannot be found")
    End If

    'check for file
    If IO.File.Exists(svPDFName) Then
        Throw New ApplicationException(svPDFName & " already exists")
    End If

    'convert
    Dim myProcInfo As New ProcessStartInfo
    myProcInfo.FileName = "C:\Program Files\GhostScript\GSWIN32C.EXE"
    myProcInfo.Arguments = "-sDEVICE=pdfwrite -q -dSAFER -dNOPAUSE -sOUTPUTFILE=""" & svPDFName & """ -dBATCH """ & svPsFileName & """"
    Debug.Print(myProcInfo.Arguments)

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

    'wait for finish (no more than 20 seconds)
    myProc.WaitForExit(20000)

    'delete PS
    If IO.File.Exists(svPDFName) Then IO.File.Delete(svPsFileName)

End Function
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top