0
#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);
    }
}
}
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Chris
  • 21
  • 1
  • 6
  • Please, could you assure if you are using Ghostscript .NET and .NET Framework? If not, and if you are using .NET Core Framework, could you confirm if you followed steps suggested by Chris Catignani? Once this is clear, could you check that the current error you are receiving is the one you post on the comment below (_"An error occured when call to 'gsapi_init_with_args' is made: -100"_)? About the error you mentioned in the post title (_CS0116 A namspace cannot directly contain members such as fields or methods on Line 3_), is the program still showing it? – Dave Miller Nov 11 '20 at 13:21

1 Answers1

0

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

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
  • The previous coder that built this built it in .NetFramework. Currently, have Ghostscript.Net installed. But getting message: Exception thrown: 'Ghostscript.NET.GhostscriptAPICallException' in Ghostscript.NET. With a error message: {"An error occured when call to 'gsapi_init_with_args' is made: -100"} – Chris Oct 01 '20 at 02:19