2

我正在尝试创建一个简单的 C# 应用程序,它可以接受 StdIn 上的输入并使用 Ghostscript 处理它以创建 PDF,最终我想对输出 PDF 做其他事情,但现在只创建 PDF 就足够了。

我正在考虑使用进程运行 Ghostscript .exe,但后来我发现了 Ghostscript.NET,我正在努力解决的问题是如何将 StdIn 上接收到的数据传递给 Ghostscript.NET 处理器。

        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));
        }

这是来自 GitHub 存储库,但我不确定它是否是我需要的:

    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);
        }
    }
4

1 回答 1

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);
}
//...
于 2018-05-09T14:13:52.527 回答