Skip to content
This repository
Browse code

No commit message

  • Loading branch information...
commit c10bdf58b60197d59eb2a7d1bf83fe8e95a9914d 1 parent 151591d
Dash Dash authored

Showing 1 changed file with 304 additions and 37 deletions. Show diff stats Hide diff stats

  1. +304 37 GhostScriptSharp/GhostscriptSharp.cs
341 GhostScriptSharp/GhostscriptSharp.cs
... ... @@ -1,6 +1,4 @@
1   -using System;
2   -using System.Collections.Generic;
3   -using System.Linq;
  1 +using System;
4 2 using System.Text;
5 3 using System.Runtime.InteropServices;
6 4
@@ -25,7 +23,29 @@ public class GhostscriptWrapper
25 23 private static extern void DeleteAPIInstance(IntPtr instance);
26 24 #endregion
27 25
28   - /// <summary>
  26 + #region Globals
  27 +
  28 + private static readonly string[] ARGS = new string[] {
  29 + // Keep gs from writing information to standard output
  30 + "-q",
  31 + "-dQUIET",
  32 +
  33 + "-dPARANOIDSAFER", // Run this command in safe mode
  34 + "-dBATCH", // Keep gs from going into interactive mode
  35 + "-dNOPAUSE", // Do not prompt and pause for each page
  36 + "-dNOPROMPT", // Disable prompts for user interaction
  37 + "-dMaxBitmap=500000000", // Set high for better performance
  38 + "-dNumRenderingThreads=4", // Multi-core, come-on!
  39 +
  40 + // Configure the output anti-aliasing, resolution, etc
  41 + "-dAlignToPixels=0",
  42 + "-dGridFitTT=0",
  43 + "-dTextAlphaBits=4",
  44 + "-dGraphicsAlphaBits=4"
  45 + };
  46 + #endregion
  47 +
  48 + /// <summary>
29 49 /// Generates a thumbnail jpg for the pdf at the input path and saves it
30 50 /// at the output path
31 51 /// </summary>
@@ -44,17 +64,40 @@ public static void GeneratePageThumbs(string inputPath, string outputPath, int f
44 64 CallAPI(GetArgs(inputPath, outputPath, firstPage, lastPage, width, height));
45 65 }
46 66
  67 + /// <summary>
  68 + /// Rasterises a PDF into selected format
  69 + /// </summary>
  70 + /// <param name="inputPath">PDF file to convert</param>
  71 + /// <param name="outputPath">Destination file</param>
  72 + /// <param name="settings">Conversion settings</param>
  73 + public static void GenerateOutput(string inputPath, string outputPath, GhostscriptSettings settings)
  74 + {
  75 + CallAPI(GetArgs(inputPath, outputPath, settings));
  76 + }
  77 +
47 78 /// <summary>
48 79 /// Calls the Ghostscript API with a collection of arguments to be passed to it
49 80 /// </summary>
50 81 private static void CallAPI(string[] args)
51 82 {
52   - // Get a pointer to an instance of the Ghostscript API and run the API with the current arguments
53   - IntPtr gsInstancePtr;
54   - CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
55   - InitAPI(gsInstancePtr, args.Length, args);
  83 + // Get a pointer to an instance of the Ghostscript API and run the API with the current arguments
  84 + IntPtr gsInstancePtr;
  85 + CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
  86 + try
  87 + {
  88 +
  89 + int result = InitAPI(gsInstancePtr, args.Length, args);
56 90
57   - Cleanup(gsInstancePtr);
  91 + if (result < 0)
  92 + {
  93 + throw new ExternalException("Ghostscript conversion error", result);
  94 + }
  95 +
  96 + }
  97 + finally
  98 + {
  99 + Cleanup(gsInstancePtr);
  100 + }
58 101 }
59 102
60 103 /// <summary>
@@ -73,36 +116,260 @@ private static void Cleanup(IntPtr gsInstancePtr)
73 116 /// <param name="outputPath">Path to the output file</param>
74 117 /// <param name="firstPage">The page of the file to start on</param>
75 118 /// <param name="lastPage">The page of the file to end on</param>
76   - private static string[] GetArgs(string inputPath, string outputPath, int firstPage, int lastPage, int width, int height)
  119 + private static string[] GetArgs(string inputPath,
  120 + string outputPath,
  121 + int firstPage,
  122 + int lastPage,
  123 + int width,
  124 + int height)
77 125 {
78   - return new[]
79   - {
80   - // Keep gs from writing information to standard output
81   - "-q",
82   - "-dQUIET",
83   -
84   - "-dPARANOIDSAFER", // Run this command in safe mode
85   - "-dBATCH", // Keep gs from going into interactive mode
86   - "-dNOPAUSE", // Do not prompt and pause for each page
87   - "-dNOPROMPT", // Disable prompts for user interaction
88   - "-dMaxBitmap=500000000", // Set high for better performance
89   -
90   - // Set the starting and ending pages
91   - String.Format("-dFirstPage={0}", firstPage),
92   - String.Format("-dLastPage={0}", lastPage),
93   -
94   - // Configure the output anti-aliasing, resolution, etc
95   - "-dAlignToPixels=0",
96   - "-dGridFitTT=0",
97   - "-sDEVICE=jpeg",
98   - "-dTextAlphaBits=4",
99   - "-dGraphicsAlphaBits=4",
100   - String.Format("-r{0}x{1}", width, height),
  126 + // To maintain backwards compatibility, this method uses previous hardcoded values.
  127 +
  128 + GhostscriptSettings s = new GhostscriptSettings();
  129 + s.Device = Settings.GhostscriptDevices.jpeg;
  130 + s.Page.Start = firstPage;
  131 + s.Page.End = lastPage;
  132 + s.Resolution = new System.Drawing.Size(width, height);
101 133
102   - // Set the input and output files
103   - String.Format("-sOutputFile={0}", outputPath),
104   - inputPath
105   - };
  134 + return GetArgs(inputPath, outputPath, s);
106 135 }
  136 +
  137 + /// <summary>
  138 + /// Returns an array of arguments to be sent to the Ghostscript API
  139 + /// </summary>
  140 + /// <param name="inputPath">Path to the source file</param>
  141 + /// <param name="outputPath">Path to the output file</param>
  142 + /// <param name="settings">API parameters</param>
  143 + /// <returns>API arguments</returns>
  144 + private static string[] GetArgs(string inputPath,
  145 + string outputPath,
  146 + GhostscriptSettings settings)
  147 + {
  148 + System.Collections.ArrayList args = new System.Collections.ArrayList(ARGS);
  149 +
  150 + if (settings.Device == Settings.GhostscriptDevices.UNDEFINED)
  151 + {
  152 + throw new ArgumentException("An output device must be defined for Ghostscript", "GhostscriptSettings.Device");
  153 + }
  154 +
  155 + if (settings.Page.AllPages == false && (settings.Page.Start <= 0 && settings.Page.End < settings.Page.Start))
  156 + {
  157 + throw new ArgumentException("Pages to be printed must be defined.", "GhostscriptSettings.Pages");
  158 + }
  159 +
  160 + if (settings.Resolution.IsEmpty)
  161 + {
  162 + throw new ArgumentException("An output resolution must be defined", "GhostscriptSettings.Resolution");
  163 + }
  164 +
  165 + if (settings.Size.Native == Settings.GhostscriptPageSizes.UNDEFINED && settings.Size.Manual.IsEmpty)
  166 + {
  167 + throw new ArgumentException("Page size must be defined", "GhostscriptSettings.Size");
  168 + }
  169 +
  170 + // Output device
  171 + args.Add(String.Format("-sDEVICE={0}", settings.Device));
  172 +
  173 + // Pages to output
  174 + if (settings.Page.AllPages)
  175 + {
  176 + args.Add("-dFirstPage=1");
  177 + }
  178 + else
  179 + {
  180 + args.Add(String.Format("-dFirstPage={0}", settings.Page.Start));
  181 + if (settings.Page.End >= settings.Page.Start)
  182 + {
  183 + args.Add(String.Format("-dLastPage={0}", settings.Page.End));
  184 + }
  185 + }
  186 +
  187 + // Page size
  188 + if (settings.Size.Native == Settings.GhostscriptPageSizes.UNDEFINED)
  189 + {
  190 + args.Add(String.Format("-dDEVICEWIDTHPOINTS={0}", settings.Size.Manual.Width));
  191 + args.Add(String.Format("-dDEVICEHEIGHTPOINTS={0}", settings.Size.Manual.Height));
  192 + }
  193 + else
  194 + {
  195 + args.Add(String.Format("-sPAPERSIZE={0}", settings.Size.Native.ToString()));
  196 + }
  197 +
  198 + // Page resolution
  199 + args.Add(String.Format("-dDEVICEXRESOLUTION={0}", settings.Resolution.Width));
  200 + args.Add(String.Format("-dDEVICEYRESOLUTION={0}", settings.Resolution.Height));
  201 +
  202 + // Files
  203 + args.Add(String.Format("-sOutputFile={0}", outputPath));
  204 + args.Add(inputPath);
  205 +
  206 + return (string[])args.ToArray(typeof(string));
  207 +
  208 + }
107 209 }
  210 +
  211 + /// <summary>
  212 + /// Ghostscript settings
  213 + /// </summary>
  214 + public class GhostscriptSettings
  215 + {
  216 + private Settings.GhostscriptDevices _device;
  217 + private Settings.GhostscriptPages _pages = new Settings.GhostscriptPages();
  218 + private System.Drawing.Size _resolution;
  219 + private Settings.GhostscriptPageSize _size = new Settings.GhostscriptPageSize();
  220 +
  221 + public Settings.GhostscriptDevices Device
  222 + {
  223 + get { return this._device; }
  224 + set { this._device = value; }
  225 + }
  226 +
  227 + public Settings.GhostscriptPages Page
  228 + {
  229 + get { return this._pages; }
  230 + set { this._pages = value; }
  231 + }
  232 +
  233 + public System.Drawing.Size Resolution
  234 + {
  235 + get { return this._resolution; }
  236 + set { this._resolution = value; }
  237 + }
  238 +
  239 + public Settings.GhostscriptPageSize Size
  240 + {
  241 + get { return this._size; }
  242 + set { this._size = value; }
  243 + }
  244 + }
108 245 }
  246 +
  247 +namespace GhostscriptSharp.Settings
  248 +{
  249 + /// <summary>
  250 + /// Which pages to output
  251 + /// </summary>
  252 + public class GhostscriptPages
  253 + {
  254 + private bool _allPages = true;
  255 + private int _start;
  256 + private int _end;
  257 +
  258 + /// <summary>
  259 + /// Output all pages avaialble in document
  260 + /// </summary>
  261 + public bool AllPages
  262 + {
  263 + set
  264 + {
  265 + this._start = -1;
  266 + this._end = -1;
  267 + this._allPages = true;
  268 + }
  269 + get
  270 + {
  271 + return this._allPages;
  272 + }
  273 + }
  274 +
  275 + /// <summary>
  276 + /// Start output at this page (1 for page 1)
  277 + /// </summary>
  278 + public int Start
  279 + {
  280 + set
  281 + {
  282 + this._allPages = false;
  283 + this._start = value;
  284 + }
  285 + get
  286 + {
  287 + return this._start;
  288 + }
  289 + }
  290 +
  291 + /// <summary>
  292 + /// Page to stop output at
  293 + /// </summary>
  294 + public int End
  295 + {
  296 + set
  297 + {
  298 + this._allPages = false;
  299 + this._end = value;
  300 + }
  301 + get
  302 + {
  303 + return this._end;
  304 + }
  305 + }
  306 + }
  307 +
  308 + /// <summary>
  309 + /// Output devices for GhostScript
  310 + /// </summary>
  311 + public enum GhostscriptDevices
  312 + {
  313 + UNDEFINED,
  314 + png16m,
  315 + pnggray,
  316 + png256,
  317 + png16,
  318 + pngmono,
  319 + pngalpha,
  320 + jpeg,
  321 + jpeggray,
  322 + tiffgray,
  323 + tiff12nc,
  324 + tiff24nc,
  325 + tiff32nc,
  326 + tiffsep,
  327 + tiffcrle,
  328 + tiffg3,
  329 + tiffg32d,
  330 + tiffg4,
  331 + tifflzw,
  332 + tiffpack,
  333 + faxg3,
  334 + faxg32d,
  335 + faxg4,
  336 + bmpmono,
  337 + bmpgray,
  338 + bmpsep1,
  339 + bmpsep8,
  340 + bmp16,
  341 + bmp256,
  342 + bmp16m,
  343 + bmp32b,
  344 + pcxmono,
  345 + pcxgray,
  346 + pcx16,
  347 + pcx256,
  348 + pcx24b,
  349 + pcxcmyk,
  350 + psdcmyk,
  351 + psdrgb,
  352 + pdfwrite,
  353 + pswrite,
  354 + epswrite,
  355 + pxlmono,
  356 + pxlcolor
  357 + }
  358 +
  359 + /// <summary>
  360 + /// Output document physical dimensions
  361 + /// </summary>
  362 + public class GhostscriptPageSize
  363 + {
  364 + private GhostscriptPageSizes _fixed;
  365 + private System.Drawing.Size _manual;
  366 +
  367 + /// <summary>
  368 + /// Custom document size
  369 + /// </summary>
  370 + public System.Drawing.Size Manual
  371 + {
  372 + set
  373 + {
  374 + this._fixed = GhostscriptPageSizes.UNDEFINED;
  375 + this._manual = value; %0

0 comments on commit c10bdf5

Please sign in to comment.
Something went wrong with that request. Please try again.