Exemple #1
0
        private static Version GetNativeVersion()
        {
            // Get the Ghostscript version.
            //      GPL Ghostscript
            //      Copyright Some Company
            //      815
            //      20040922
            // Extracted as a structure but we're only interested in the 3rd field (815).
            GhostscriptNativeMethods.GhostscriptVersion ghostscriptVersion = new GhostscriptNativeMethods.GhostscriptVersion();
            try
            {
                GhostscriptNativeMethods.GetRevision(ref ghostscriptVersion, Marshal.SizeOf(ghostscriptVersion));
            }
            catch (DllNotFoundException)
            {
                return(null);
            }
            catch (MissingMethodException)
            {
                return(null);
            }

            // Convert version from integer such as '914' to Version object 9.14
            string versionText = ((double)ghostscriptVersion.Revision / 100).ToString(CultureInfo.InvariantCulture);

            return(new Version(versionText));
        }
Exemple #2
0
        public string Execute(GhostscriptSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            // Flatten settings into Ghostscript arguments
            string[] arguments = GetArguments(settings);

            // Use callback handlers to capture stdout and stderr.
            // NOTE: Delegates do not need to be pinned.
            StringBuilder outputBuilder = new StringBuilder();
            StringBuilder errorBuilder  = new StringBuilder();
            GhostscriptMessageEventHandler outputHandler = (i, s, l) => HandleOutputMessage(outputBuilder, s, l);
            GhostscriptMessageEventHandler errorHandler  = (i, s, l) => HandleOutputMessage(errorBuilder, s, l);
            GhostscriptMessageEventHandler inputHandler  = (i, s, l) => l;

            // NOTE: Ghostscript supports only one instance per process
            int result;

            lock (_syncObject)
            {
                // Create a new instance of Ghostscript. This instance is passed to most other gsapi functions.
                // The caller_handle will be provided to callback functions.
                IntPtr instance;
                GhostscriptNativeMethods.NewInstance(out instance, IntPtr.Zero);

                // Set the callback functions for stdio.
                GhostscriptNativeMethods.SetMessageHandlers(instance, inputHandler, outputHandler, errorHandler);

                // Initialise the interpreter.
                // This calls gs_main_init_with_args() in imainarg.c. See below for return codes.
                // The arguments are the same as the "C" main function: argv[0] is ignored and the user supplied arguments are
                // argv[1] to argv[argc-1].
                result = GhostscriptNativeMethods.InitializeWithArguments(instance, arguments.Length, arguments);

                // Exit the interpreter.
                // This must be called on shutdown if gsapi_init_with_args() has been called, and just before gsapi_delete_instance().
                GhostscriptNativeMethods.Exit(instance);

                // Destroy an instance of Ghostscript.
                // Before you call this, Ghostscript must have finished.
                // If Ghostscript has been initialised, you must call gsapi_exit before gsapi_delete_instance.
                GhostscriptNativeMethods.DeleteInstance(instance);
            }

            // Check for errors. Zero and e_Quit(-101) are not errors.
            string output = outputBuilder.ToString();

            if (result != 0 && result != -101)
            {
                // Use error as message if output is empty
                string error = errorBuilder.ToString();
                if (string.IsNullOrEmpty(output))
                {
                    output = error;
                }

                GhostscriptException exception = new GhostscriptException(output);
                exception.Data["args"]   = arguments;
                exception.Data["stderr"] = error;
                throw exception;
            }

            // Return the output message
            return(output);
        }