Exemplo n.º 1
0
        private static string[] GetArguments(GhostscriptSettings settings)
        {
            // Flatten Ghostscript engine settings into string array
            // - first argument ignore (argv[0])
            // - input path as last argument
            string[]      arguments;
            List <string> argumentItems = new List <string> {
                string.Empty
            };

            foreach (string key in settings.AllKeys)
            {
                string[] values = settings.GetValues(key);
                if (values == null)
                {
                    // No value
                    argumentItems.Add(key);
                }
                else
                {
                    IEnumerable <string> items = values.Select(value => string.Concat(key, value));
                    argumentItems.AddRange(items);
                }
            }
            arguments = argumentItems.ToArray();
            return(arguments);
        }
Exemplo n.º 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);
        }