1

I am doing some pdf processing with GhostScript, But I have found some strange issue when I have a file name for example 2. BD tools.pdf or 3 Amendment 1_2013.pdf GhostScript sames to have some issues to open these specific files, and I wanted to know if I maybe missed some argument or how to at least if these files are approached how to ignore them ?

  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);
            }
        }
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Niko.K
  • 297
  • 4
  • 17
  • why was this down voted? – Niko.K Nov 01 '16 at 12:14
  • FYI I didn't delete the comment. Maybe it was flagged and then deleted by a moderator. Comments are designed to be transient, so that's nothing to worry about. I don't think it's funny to call me a pathetic clown loser (I have access to deleted questions and can still see it). Please calm down. – Thomas Weller Nov 03 '16 at 13:48
  • ...`-r500 \"-o" + output + "%d.jpg\" \"" + input + "\"";` – Thomas Weller Nov 03 '16 at 13:50

2 Answers2

2

Specify the FileName and WorkingDirectory like this.

proc.StartInfo.FileName = Path.GetFileName(ghostScriptPath);
proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(ghostScriptPath);
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • Thanks for you answer, may I ask for a explanation ? Why is that making a difference ? – Niko.K Nov 01 '16 at 11:12
  • @Niko.K For your current case is no big difference, but Path.GetFileName and Path.GetDirectoryName can remove some faulty characters from your path. When you tried did you have any problems ? IF you se UseShellExecute to false and your Directory is empty it takes Environment.CurrentDirectory; – mybirthname Nov 01 '16 at 11:28
  • With adding your proposed solution I get the error that {The system cannot find the file specified"} If the system tries to open a 2. BD tools.pdf or 3 Amendment 1_2013.pdf – Niko.K Nov 01 '16 at 12:10
  • 1
    I seriously dont see the point how your answer got 2 up votes by simply being unhelpful ... – Niko.K Nov 03 '16 at 09:28
2

You are effectively invoking Ghostscript just as you would from a shell, so a space character in the argument list will be interpreted as the end of one argument and the beginning of the next.

The way to avoid this is, of course, to put "" around the filename. Obviously you will have to escape the quotes in your string.

So it isn't Ghostscript which has a problem here (GS is perfectly capable of dealing with such filenames), its the fact that you haven't catered for the requirements of shell processing.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Thanks allot for you explanation, could you help me how would / should I handle that in that case ? – Niko.K Nov 01 '16 at 12:12
  • You need to put a " and a " around the filename which contains spaces in the string you are creating. Clearly the String object uses " as a delimiter as well, so you will have to escape it in some fashion. Since I don't use C# I have no idea how you should do that. – KenS Nov 01 '16 at 15:29
  • Dont get me wrong its quit cool that someone from Ghostscript or at lest the company that is in charge wrote back to my question. But as you may see the question is clearly tagged with 'C#' so why bother posing a answer when you dont have a clue how to answer my question. You answer could have simply been a link to your page or what ever .. – Niko.K Nov 02 '16 at 12:47
  • 1
    I thought that it may not be clear to you what the problem is, since you clearly think its a Ghostscript problem, and it isn't. Since you've **also** tagged it with C# perhaps someone reading those tags will answer your question. Seems like a pretty basic sort of problem to me though. Two seconds Googling points to this answer http://stackoverflow.com/questions/3905946/how-to-add-doublequotes-to-a-string-that-is-inside-a-variable – KenS Nov 02 '16 at 14:45
  • In that case why dont you edit your answer and let me set it as the correct answer ? – Niko.K Nov 03 '16 at 08:42
  • 1
    Frankly I can't be bothered. I answered the Ghostscript part of the question, and the answer is correct from that point of view. If that wasn't the question you were asking, then you shouldn't have tagged it with Ghostscript. I'm not an expert in every computer language in existence, so the task of implementation is left as an exercise for the student. It wasn't hard to figure out after all. – KenS Nov 03 '16 at 12:26