Example #1
0
        /// <summary>
        /// Releases all resources used by the Ghostscript.NET.GhostscriptInterpreter instance.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // GSAPI: exit the interpreter
                    _gs.gsapi_exit(_gs_instance);

                    // GSAPI: destroy an instance of Ghostscript
                    _gs.gsapi_delete_instance(_gs_instance);

                    // release all resource used by Ghostscript library
                    _gs.Dispose();
                }

                // check if the display device callback handler is attached
                if (_displayDevice_callback_handle != IntPtr.Zero)
                {
                    // free earlier allocated memory used for the display device callback
                    Marshal.FreeCoTaskMem(_displayDevice_callback_handle);
                }

                _disposed = true;
            }
        }
Example #2
0
        /// <summary>
        /// Run Ghostscript.
        /// </summary>
        /// <param name="args">Command arguments</param>
        /// <param name="stdIO_callback">StdIO callback, can be set to null if you dont want to handle it.</param>
        public void Process(string[] args, GhostscriptStdIO stdIO_callback)
        {
            IntPtr instance = IntPtr.Zero;

            int rc_ins = _gs.gsapi_new_instance(out instance, IntPtr.Zero);

            if (ierrors.IsError(rc_ins))
            {
                throw new GhostscriptAPICallException("gsapi_new_instance", rc_ins);
            }

            try
            {
                _stdIO_Callback = stdIO_callback;

                _internalStdIO_Callback = new GhostscriptProcessorInternalStdIOHandler(
                    new StdInputEventHandler(OnStdIoInput),
                    new StdOutputEventHandler(OnStdIoOutput),
                    new StdErrorEventHandler(OnStdIoError));

                int rc_stdio = _gs.gsapi_set_stdio(instance,
                                                   _internalStdIO_Callback._std_in,
                                                   _internalStdIO_Callback._std_out,
                                                   _internalStdIO_Callback._std_err);

                if (ierrors.IsError(rc_stdio))
                {
                    throw new GhostscriptAPICallException("gsapi_set_stdio", rc_stdio);
                }

                int rc_init = _gs.gsapi_init_with_args(instance, args.Length, args);

                if (ierrors.IsErrorIgnoreQuit(rc_init))
                {
                    throw new GhostscriptAPICallException("gsapi_init_with_args", rc_init);
                }

                int rc_exit = _gs.gsapi_exit(instance);

                if (ierrors.IsErrorIgnoreQuit(rc_exit))
                {
                    throw new GhostscriptAPICallException("gsapi_exit", rc_exit);
                }
            }
            finally
            {
                _gs.gsapi_delete_instance(instance);
            }
        }
Example #3
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _gs.gsapi_exit(_gs_instance);

                    _gs.gsapi_delete_instance(_gs_instance);

                    _gs.Dispose();
                }

                if (_displayDevice_callback_handle != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(_displayDevice_callback_handle);
                }

                _disposed = true;
            }
        }
Example #4
0
        /// <summary>
        /// Run Ghostscript.
        /// </summary>
        /// <param name="args">Command arguments</param>
        /// <param name="stdIO_callback">StdIO callback, can be set to null if you dont want to handle it.</param>
        public void StartProcessing(string[] args, GhostscriptStdIO stdIO_callback)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            if (args.Length < 3)
            {
                throw new ArgumentOutOfRangeException("args");
            }

            for (int i = 0; i < args.Length; i++)
            {
                args[i] = System.Text.Encoding.Default.GetString(System.Text.Encoding.UTF8.GetBytes(args[i]));
            }

            _isRunning = true;

            IntPtr instance = IntPtr.Zero;

            int rc_ins = _gs.gsapi_new_instance(out instance, IntPtr.Zero);

            if (ierrors.IsError(rc_ins))
            {
                throw new GhostscriptAPICallException("gsapi_new_instance", rc_ins);
            }

            try
            {
                _stdIO_Callback = stdIO_callback;

                _internalStdIO_Callback = new GhostscriptProcessorInternalStdIOHandler(
                    new StdInputEventHandler(OnStdIoInput),
                    new StdOutputEventHandler(OnStdIoOutput),
                    new StdErrorEventHandler(OnStdIoError));

                int rc_stdio = _gs.gsapi_set_stdio(instance,
                                                   _internalStdIO_Callback._std_in,
                                                   _internalStdIO_Callback._std_out,
                                                   _internalStdIO_Callback._std_err);

                _poolCallBack = new gsapi_pool_callback(Pool);

                int rc_pool = _gs.gsapi_set_poll(instance, _poolCallBack);

                if (ierrors.IsError(rc_pool))
                {
                    throw new GhostscriptAPICallException("gsapi_set_poll", rc_pool);
                }

                if (ierrors.IsError(rc_stdio))
                {
                    throw new GhostscriptAPICallException("gsapi_set_stdio", rc_stdio);
                }

                this.OnStarted(new GhostscriptProcessorEventArgs());

                _stopProcessing = false;

                if (_gs.is_gsapi_set_arg_encoding_supported)
                {
                    int rc_enc = _gs.gsapi_set_arg_encoding(instance, GS_ARG_ENCODING.UTF8);
                }

                int rc_init = _gs.gsapi_init_with_args(instance, args.Length, args);

                if (ierrors.IsErrorIgnoreQuit(rc_init))
                {
                    if (!ierrors.IsInterrupt(rc_init))
                    {
                        throw new GhostscriptAPICallException("gsapi_init_with_args", rc_init);
                    }
                }

                int rc_exit = _gs.gsapi_exit(instance);

                if (ierrors.IsErrorIgnoreQuit(rc_exit))
                {
                    throw new GhostscriptAPICallException("gsapi_exit", rc_exit);
                }
            }
            finally
            {
                _gs.gsapi_delete_instance(instance);

                GC.Collect();

                _isRunning = false;

                this.OnCompleted(new GhostscriptProcessorEventArgs());
            }
        }
        /// <summary>
        /// Run Ghostscript.
        /// </summary>
        /// <param name="args">Command arguments</param>
        /// <param name="stdIO_callback">StdIO callback, can be set to null if you dont want to handle it.</param>
        public void StartProcessing(string[] args, GhostscriptStdIO stdIO_callback = null)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            if (args.Length < 3)
            {
                throw new ArgumentOutOfRangeException("args");
            }

            for (int i = 0; i < args.Length; i++)
            {
                args[i] = System.Text.Encoding.Default.GetString(System.Text.Encoding.UTF8.GetBytes(args[i]));
            }

            _isRunning = true;

            IntPtr instance = IntPtr.Zero;

            int rc_ins = _gs.gsapi_new_instance(out instance, IntPtr.Zero);

            if (ierrors.IsError(rc_ins))
            {
                throw new GhostscriptAPICallException("gsapi_new_instance", rc_ins, _internalStdIO_Callback);
            }

            try
            {
                _stdIO_Callback = stdIO_callback;

                _internalStdIO_Callback = new GhostscriptProcessorInternalStdIOHandler(
                    new StdInputEventHandler(OnStdIoInput),
                    new StdOutputEventHandler(OnStdIoOutput),
                    new StdErrorEventHandler(OnStdIoError));

                int rc_stdio = _gs.gsapi_set_stdio(instance,
                                                   _internalStdIO_Callback._std_in,
                                                   _internalStdIO_Callback._std_out,
                                                   _internalStdIO_Callback._std_err);

                _poolCallBack = new gsapi_pool_callback(Pool);

                int rc_pool = _gs.gsapi_set_poll(instance, _poolCallBack);

                if (ierrors.IsError(rc_pool))
                {
                    throw new GhostscriptAPICallException("gsapi_set_poll", rc_pool, _internalStdIO_Callback);
                }

                if (ierrors.IsError(rc_stdio))
                {
                    throw new GhostscriptAPICallException("gsapi_set_stdio", rc_stdio, _internalStdIO_Callback);
                }

                this.OnStarted(new GhostscriptProcessorEventArgs());

                _stopProcessing = false;

                if (_gs.is_gsapi_set_arg_encoding_supported)
                {
                    int rc_enc = _gs.gsapi_set_arg_encoding(instance, GS_ARG_ENCODING.UTF8);
                }

                int rc_init = _gs.gsapi_init_with_args(instance, args.Length, args);

                if (ierrors.IsErrorIgnoreQuit(rc_init))
                {
                    if (!ierrors.IsInterrupt(rc_init))
                    {
                        throw new GhostscriptAPICallException("gsapi_init_with_args", rc_init, _internalStdIO_Callback);
                    }
                }
            }
            finally
            {
                // gsapi_exit() :
                //
                // Exit the interpreter. This MUST be called on shutdown if gsapi_init_with_args() has been called, and just before gsapi_delete_instance().
                //
                // ^^^ that's from the docs at https://ghostscript.com/doc/9.52/API.htm#exit (emphasis mine): it's placed in the `finally` clause
                // section here to ensure it is called when rc_init == e_Fatal (or other error) occurs and throws an exception in the code chunk above.
                try
                {
                    int rc_exit = _gs.gsapi_exit(instance);

                    if (ierrors.IsErrorIgnoreQuit(rc_exit))
                    {
                        throw new GhostscriptAPICallException("gsapi_exit", rc_exit, _internalStdIO_Callback);
                    }
                }
                finally
                {
                    _gs.gsapi_delete_instance(instance);

                    GC.Collect();

                    _isRunning = false;

                    this.OnCompleted(new GhostscriptProcessorEventArgs());
                }
            }
        }