GhostScript在打开名称中包含空格的文件时遇到问题

时间:2016-11-01 11:00:26

标签: c# pdf ghostscript

我正在使用GhostScript进行一些pdf处理,但是当我有一个文件名例如 2时,我发现了一些奇怪的问题。 BD tools.pdf或3修正案1_2013.pdf GhostScript同样有问题打开这些特定文件,我想知道我是否可能错过了一些争论或至少如果这些文件是接近如何忽略它们?

  public static void PdfToJpg(string ghostScriptPath, string input, string output)
        {
            try
            {
                //To convert a figure to an image file: and to render the same image at 500dpi
                String ars = "-dNOPAUSE -sDEVICE=jpeg -r500 -o" + output + "%d.jpg " + input;
                Process proc = new Process();
                proc.StartInfo.FileName = ghostScriptPath;
                proc.StartInfo.Arguments = ars;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.Start();
                proc.WaitForExit();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

2 个答案:

答案 0 :(得分:2)

像这样指定FileNameWorkingDirectory

proc.StartInfo.FileName = Path.GetFileName(ghostScriptPath);
proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(ghostScriptPath);

答案 1 :(得分:2)

您正在有效地调用Ghostscript,就像在shell中一样,因此参数列表中的空格字符将被解释为一个参数的结尾和下一个参数的开头。

避免这种情况的方法当然是放置""文件名周围。显然你必须逃避字符串中的引号。

所以Ghostscript在这里没有问题(GS完全有能力处理这样的文件名),事实上你还没有满足shell处理的要求。