#nullable enable
using System.Text;
using Ghostscript.NET;
using Ghostscript.NET.Processor;
namespace Metro.MbaProcessing.Core
{
internal static class PdfToText
{
private const string HandleTag = "%handle%";
private const string HandleFormat = "X2";
internal static string Process(string filePath, Encoding encoding)
{
GhostscriptVersionInfo gsv = GhostscriptVersionInfo.GetLastInstalledVersion();
using var processor = new GhostscriptProcessor(gsv);
using var pipedOutput = new GhostscriptPipedOutput();
string outputPipeHandle = $"{HandleTag}{int.Parse(pipedOutput.ClientHandle).ToString(HandleFormat)}";
string[] switches =
{
$"-o{outputPipeHandle}",
"-empty",
"-dQUIET",
"-dSAFER",
"-dBATCH",
"-dNOPAUSE",
"-dNOPROMPT",
"-sDEVICE=txtwrite",
//$"-o{outputPipeHandle}",
"-q",
"-f",
filePath
};
processor.StartProcessing(switches, null);
return encoding.GetString(pipedOutput.Data);
}
}
}