2

I'm attempting to create a simple C# application which can accept input on StdIn and process that with Ghostscript to create a PDF, eventually I'd like to do other things with the output PDF, but for now just creating the PDF is enough.

I was thinking of running the Ghostscript .exe with a Process, but then I found Ghostscript.NET, the thing I'm struggling with is how to pass the data received on StdIn to the Ghostscript.NET Processor.

        using (GhostscriptProcessor ghostscript = new GhostscriptProcessor(gvi))
        {
            List<string> switches = new List<string>();
            switches.Add("-sDEVICE=pdfwrite");
            switches.Add("-r300");
            switches.Add("-dBATCH");
            switches.Add("-dNOPAUSE");
            switches.Add("-dSAFER");
            switches.Add("-dNOPROMPT");
            switches.Add("-sPAPERSIZE=a4");
            switches.Add("-sOutputFile = \"" + filename + "\"");
            switches.Add("-c");
            switches.Add(".setpdfwrite");
            switches.Add(@"-f");
            switches.Add("-");

            ghostscript.Process(switches.ToArray(), new ConsoleStdIO(true, true, true));
        }

This was from the GitHub repo, but I'm not sure if it's what I need or not:

    public class ConsoleStdIO : Ghostscript.NET.GhostscriptStdIO
    {
        public ConsoleStdIO(bool handleStdIn, bool handleStdOut, bool handleStdErr) : base(handleStdIn, handleStdOut, handleStdErr) { }

        public override void StdIn(out string input, int count)
        {
            char[] userInput = new char[count];
            Console.In.ReadBlock(userInput, 0, count);
            input = new string(userInput);
        }

        public override void StdOut(string output)
        {
            Console.Write(output);
        }

        public override void StdError(string error)
        {
            Console.Write(error);
        }
    }
playworker
  • 43
  • 1
  • 5

1 Answers1

0
public static string[] PStoPDFArguments(string fileName) =>
    new string[]
    {
        "-dBATCH",
        "-dNOPAUSE",
        "-sDEVICE=pdfwrite",
        "-sPAPERSIZE=a4",
        "-dPDFSETTINGS=/prepress",
        $"-sOutputFile=\"{fileName}\"",
        "-"
    };


//...
public override void StdIn(out string input, int count)
{
    var buffer = new char[count];

    Console.In.ReadBlock(buffer, 0, count);

    input = buffer[0] == '\0'
          ? null
          : new string(buffer);
}
//...
Marc.Adans
  • 203
  • 2
  • 4