繁体   English   中英

Ghostscript.NET如何处理StdIn上的输入

[英]Ghostscript.NET how to handle input on StdIn

我试图创建一个简单的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);
        }
    }
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);
}
//...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM