public static string[] GetFormattedArguments(IDictionary <GhostScriptCommand, object> args)
        {
            List <string> translatedArgs;

            translatedArgs = new List <string>();
            foreach (KeyValuePair <GhostScriptCommand, object> pair in args)
            {
                translatedArgs.Add(GhostScriptAPI.GetFormattedArgument(pair.Key, pair.Value));
            }

            return(translatedArgs.ToArray());
        }
        public List <string> ConvertPdfPageToImage(string pdfFilePath, string pdfPassword, string outputdir)
        {
            if (_pdf2ImgCache.ContainsKey(pdfFilePath))
            {
                return(_pdf2ImgCache[pdfFilePath]);
            }

            List <string> imgPathList = new List <string>();

            try
            {
                string exedir      = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string businessdir = System.IO.Path.Combine(exedir, "Business");

                string path = System.IO.Path.Combine(businessdir, outputdir);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                int pageCount = GetPdfPageCount(pdfFilePath);
                for (int pageNum = 1; pageNum <= pageCount; pageNum++)
                {
                    GhostScriptAPI api = new GhostScriptAPI();
                    string         _imgFileFullPath = string.Format(@"{0}\{1}{2}", path, Guid.NewGuid(), _settings.Extension);
                    api.Execute(GetConversionArguments(pdfFilePath, _imgFileFullPath, pageNum, pdfPassword, _settings));
                    imgPathList.Add(_imgFileFullPath);
                    api.Dispose();
                }
            }
            catch (Exception ex)
            {
                return(null);
            }

            _pdf2ImgCache.Add(pdfFilePath, imgPathList);

            return(imgPathList);
        }
        public static string GetFormattedArgument(GhostScriptCommand command, object value)
        {
            string commandName;
            string operatorChar;
            string commandValue;

            if (value != null)
            {
                if (value is Enum)
                {
                    value = (int)value;
                }
                else if (value is bool)
                {
                    value = (bool)value ? "true" : "false";
                }
            }

            commandName  = GhostScriptAPI.GetCommandName(command);
            operatorChar = value == null || string.IsNullOrEmpty(commandName) || command == GhostScriptCommand.Resolution ? string.Empty : "=";
            commandValue = value == null ? string.Empty : value.ToString();

            return(commandName + operatorChar + commandValue);
        }
 public int Execute(IDictionary <GhostScriptCommand, object> args)
 {
     return(this.Execute(GhostScriptAPI.GetFormattedArguments(args)));
 }
        protected virtual IDictionary <GhostScriptCommand, object> GetConversionArguments(
            string pdfFileName, string outputImageFileName, int pageNum, string password, PdfToImageConfig settings)
        {
            Dictionary <GhostScriptCommand, object> arguments;

            arguments = new Dictionary <GhostScriptCommand, object>();

            arguments.Add(GhostScriptCommand.Silent, null);
            arguments.Add(GhostScriptCommand.Safer, null);
            arguments.Add(GhostScriptCommand.Batch, null);
            arguments.Add(GhostScriptCommand.NoPause, null);

            arguments.Add(GhostScriptCommand.Device, GhostScriptAPI.GetDeviceName(settings.ImageFormat));
            arguments.Add(GhostScriptCommand.OutputFile, outputImageFileName);

            arguments.Add(GhostScriptCommand.FirstPage, pageNum);
            arguments.Add(GhostScriptCommand.LastPage, pageNum);

            arguments.Add(GhostScriptCommand.UseCIEColor, null);

            if (settings.AntiAliasMode != AntiAliasMode.None)
            {
                arguments.Add(GhostScriptCommand.TextAlphaBits, settings.AntiAliasMode);
                arguments.Add(GhostScriptCommand.GraphicsAlphaBits, settings.AntiAliasMode);
            }

            arguments.Add(GhostScriptCommand.GridToFitTT, settings.GridFitMode);

            if (settings.TrimMode != PdfTrimMode.PaperSize)
            {
                arguments.Add(GhostScriptCommand.Resolution, settings.Dpi.ToString());
            }

            switch (settings.TrimMode)
            {
            case PdfTrimMode.PaperSize:
                if (settings.PaperSize != PaperSize.Default)
                {
                    arguments.Add(GhostScriptCommand.FixedMedia, true);
                    arguments.Add(GhostScriptCommand.PaperSize, settings.PaperSize);
                }
                break;

            case PdfTrimMode.TrimBox:
                arguments.Add(GhostScriptCommand.UseTrimBox, true);
                break;

            case PdfTrimMode.CropBox:
                arguments.Add(GhostScriptCommand.UseCropBox, true);
                break;
            }

            if (!string.IsNullOrEmpty(password))
            {
                arguments.Add(GhostScriptCommand.PDFPassword, password);
            }

            arguments.Add(GhostScriptCommand.InputFile, pdfFileName);

            return(arguments);
        }