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

There are 1 best solutions below

1
On

You need to load the ghostcript nuget package.

Click on tools.
Nuget package manager.
Manage Nuget Packages for solution.
Click Browse Tab
Search for GhostScript.NetCore by Stephan Jimane
Install