Send output to variable in place of file

Feb 18, 2015 at 1:12 AM
Edited Feb 18, 2015 at 1:13 AM
I have the following code that simply converts the first page of a pdf to text.
        Dim gVER As GhostscriptVersionInfo = _
                    GhostscriptVersionInfo.GetLastInstalledVersion(Ghostscript.NET.GhostscriptLicense.GPL, _
                                                                   Ghostscript.NET.GhostscriptLicense.GPL)

        Dim gP As New Ghostscript.NET.Processor.GhostscriptProcessor(gVER, True)

        Dim Myargs As New List(Of String)
        With Myargs
            .Add("-q")
            .Add("-dSAFER")
            .Add("-dBATCH")
            .Add("-dNOPAUSE")
            .Add("-dNOPROMPT")
            .Add("-dFirstPage=1")
            .Add("-dLastPage=1")
            .Add("-sDEVICE=txtwrite")
            .Add("-sOutputFile=c:\MyFolder\output.txt")
            .Add("-fc:\MyFolder\hello.pdf")
        End With

        gP.Process(Myargs.ToArray)
After above is done, I have to read the output file back into a string variable.

Is there anyway to send the output into some kind of file stream variable in place of sending out to a text file? (I actually don’t need to keep nor want the text file).

Open to any suggestions.

Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
Coordinator
Feb 18, 2015 at 4:44 AM
Hi Albert,

You can achieve that by using pipes. Here is C# example:
using Ghostscript.NET.Processor;

namespace Ghostscript.NET.Samples
{
    /// <summary>
    /// Sample that demonstrates how to tell Ghostscript to write the output result to 
    /// an anonymous pipe (memory) instead of the writing it to the disk.
    /// </summary>
    public class PipedOutputSample : ISample
    {
        public void Start()
        {
            string inputFile = @"E:\__test_data\test2.pdf";

            GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

            // pipe handle format: %handle%hexvalue
            string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");

            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                List<string> switches = new List<string>();
                switches.Add("-empty");
                switches.Add("-dQUIET");
                switches.Add("-dSAFER");
                switches.Add("-dBATCH");
                switches.Add("-dNOPAUSE");
                switches.Add("-dNOPROMPT");
                switches.Add("-sDEVICE=txtwrite");
                switches.Add("-o" + outputPipeHandle);
                switches.Add("-q");
                switches.Add("-f");
                switches.Add(inputFile);

                try
                {
                    processor.StartProcessing(switches.ToArray(), null);

                    byte[] rawDocumentData = gsPipedOutput.Data;

                    string txtOutput = System.Text.Encoding.UTF8.GetString(rawDocumentData);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    gsPipedOutput.Dispose();
                    gsPipedOutput = null;
                }
            }
        }
    }
}
I hope that helps.

Cheers,
Josip
Feb 19, 2015 at 2:07 AM
That does indeed work. Thank you VERY kindly for this.

I do have a number of pdf files that errors out if I don’t restrict the page range 1 to 1.

I will see if I can follow up if they are just bad pdf files (and thus really a GS issue, not your code)

And for the readers, here is the vb.net version.
        Dim strPDF As String = "c:\MyFolder\b.pdf"

        Dim gP As New Ghostscript.NET.Processor.GhostscriptProcessor()
        Dim gpH As New Ghostscript.NET.GhostscriptPipedOutput
        Dim outputPieHande As String = "%handle%" + CInt(gpH.ClientHandle).ToString("X2")

        Dim Myargs As New List(Of String)
        With Myargs
            .Add("-empty")
            .Add("-dSAFER")
            .Add("-dBATCH")
            .Add("-dNOPAUSE")
            .Add("-dNOPROMPT")
            .Add("-dFirstPage=1")
            .Add("-dLastPage=1")
            .Add("-sDEVICE=txtwrite")
            .Add("-o" & outputPieHande)
            .Add("-q")
            .Add("-f")
            .Add(strPDF)
        End With

        gP.StartProcessing(Myargs.ToArray, Nothing)

        Dim raw As Byte() = gpH.Data
        Dim strOut = System.Text.Encoding.UTF8.GetString(raw)
        gpH.Dispose()

        MsgBox(strOut)
Once again, a real kind thumbs up for your short example, as I am sure many will benefit from this.

Regards,
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada