/// <summary> /// Returns the given command and value as a formatted switch for use with the GhostScript API /// </summary> /// <param name="command"> The command. </param> /// <param name="value"> The value. </param> /// <returns> /// The format switch. /// </returns> public static string GetFormattedArgument(GhostScriptCommand command, object value) { string commandName; string operatorChar; string commandValue; // reformat some values depending on their type if (value != null) { if (value is Enum) { value = (int)value; } else if (value is bool) { value = (bool)value ? "true" : "false"; } } // and work out how the names/values will be combined 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 void ConvertPdfPageToImage(string outputFileName, int pageNumber) { if (pageNumber < 1 || pageNumber > this.PageCount) { throw new ArgumentException("Page number is out of bounds", "pageNumber"); } using (GhostScriptAPI api = new GhostScriptAPI()) api.Execute(this.GetConversionArguments(this._pdfFileName, outputFileName, pageNumber, this.PdfPassword, this.Settings)); }
/// <summary> /// Returns the given argument dictionary as an array of formatted switches for use with the GhostScript API /// </summary> /// <param name="args"> The arguments. </param> /// <returns> /// The formatted arguments. /// </returns> 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()); }
/// <summary> /// Calls the gsapi_init_with_args GhostScript API with the given arguments. /// </summary> /// <exception cref="GhostScriptException"> Thrown when ghostscript. </exception> /// <param name="args"> The arguments. </param> /// <returns> /// The GhostScript result code /// </returns> public int Execute(IDictionary <GhostScriptCommand, object> args) { return(this.Execute(GhostScriptAPI.GetFormattedArguments(args))); }
protected virtual IDictionary <GhostScriptCommand, object> GetConversionArguments(string pdfFileName, string outputImageFileName, int pageNumber, string password, Pdf2ImageSettings settings) { Dictionary <GhostScriptCommand, object> arguments; arguments = new Dictionary <GhostScriptCommand, object>(); // basic GhostScript setup arguments.Add(GhostScriptCommand.Silent, null); arguments.Add(GhostScriptCommand.Safer, null); arguments.Add(GhostScriptCommand.Batch, null); arguments.Add(GhostScriptCommand.NoPause, null); // specify the output arguments.Add(GhostScriptCommand.Device, GhostScriptAPI.GetDeviceName(settings.ImageFormat)); arguments.Add(GhostScriptCommand.OutputFile, outputImageFileName); // page numbers arguments.Add(GhostScriptCommand.FirstPage, pageNumber); arguments.Add(GhostScriptCommand.LastPage, pageNumber); // graphics options //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); // image size 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; } // pdf password if (!string.IsNullOrEmpty(password)) { arguments.Add(GhostScriptCommand.PDFPassword, password); } // pdf filename arguments.Add(GhostScriptCommand.InputFile, pdfFileName); return(arguments); }