Convert A PDF Into A Series of Images Using C# and GhostScript
Convert A PDF Into A Series of Images Using C# and GhostScript
http://www.codeproject.com/ rtic!es/"#$$%%/Convert&a...
Sign in
home
articles
quick answers
discussions
features
community
help
how to create a document with multiple pages per sheet usin ;e<t
Articles Web Development !"rts, #r"p!s "n$ %m"ges %m"ges "n$ m<ime$i"
Article 'ro(se o$e St"ts )evisions (5) Altern"tives omments * Disc&ssions (17)
About Article
A Technical Blog, origin"ll+ poste$ "t !ttp,--c+ote./com -blog-convert0"0p$10into0"0 series0o10im"ges0&sing0 cs!"rp0 "n$0g!ostscript2so&rce3rss 4o( to convert " 5D6 into " series o1 im"ges &sing 7 "n$ #!ostScript 8+pe Technical Blog CPOL 20 an 20!2 ""#$%% 2#!%& 2% times
4ntroduction
9icence
An "pplic"tion % ("s recentl+ (or.ing on receive$ 5D6 1iles 1rom " (ebservice (!ic! it t!en nee$e$ to store in " $"t"b"se/ % ("nte$ t!e "bilit+ to $ispl"+ previe(s o1 t!ese documents (it!in t!e "pplic"tion/ W!ile t!ere "re " n&mber o1 sol&tions 1or cre"ting 5D6 1iles 1rom 7, options 1or vie(ing " 5D6 (it!in +o&r "pplic"tion is m&c! more limite$, &nless +o& p&rc!"se e<pensive commerci"l pro$&cts, or &se >? interop to embe$ Acrob"t )e"$er into +o&r "pplic"tion/ 8!is "rticle $escribes "n "ltern"te sol&tion, in (!ic! t!e pages in " 5D6 "re converte$ into im"ges using 'host(cript, 1rom (!ere +o& c"n t!en $ispl"+ t!em in +o&r "pplic"tion/
%n or$er to "voi$ !&ge ("lls o1 te<t, t!is "rticle !"s been split into t(o p"rts, t!e 1irst $e"ling (it! t!e "ct&"l conversion o1 " 5D6, "n$ t!e secon$ $emonstr"tes !o( to e<ten$ t!e %m"ge'o< control to $ispl"+ t!e im"ges/
Ca2eat -mptor
'e1ore (e st"rt, some @&ic. points, 8!e met!o$ %Am "bo&t to $emonstr"te converts t!e p"ge o1 t!e 5D6 into "n im"ge/ 8!is me"ns t!"t it is ver+ s&it"ble 1or vie(ing, b&t inter"ctive elements s&c! "s 1orms, !+perlin.s "n$ even goo$ ol$ te<t selection "re not "v"il"ble/ 'host(cript !"s " n&mber o1 licenses "ssoci"te$ (it! it b&t % c"nAt 1in$ "n+ in1orm"tion o1 t!e pricing o1 commerci"l licenses/ 8!e 'host(cript A5% %ntegr"tion libr"r+ &se$ b+ t!is proBect isnAt complete "n$ %Am not going to go into t!e bells "n$ (!istles o1 !o( it (or.s in t!is p"ir o1 "rticles 0 once %Ave complete$ t!e o&tst"n$ing 1&nction"lit+, %All create " ne( "rticle 1or it/
'etting (tarted
Co& c"n $o(nlo"$ t!e t(o libr"ries &se$ in t!is "rticle 1rom t!e lin.s belo(, t!ese "re,
# of $
#/'(/'%#) (:'$ P*
http://www.codeproject.com/ rtic!es/"#$$%%/Convert&a...
Cyotek.GhostScript 0 core libr"r+ provi$ing 'host(cript integr"tion s&pport Cyotek.GhostScript.PdfConversion 0 s&pport libr"r+ 1or converting " 5D6 document into im"ges
Please note that the native GhostScript DLL is not included in these downloads, you will need to obtain that from the GhostScript project page.
Top News
Win$o(s D rele"se $"te, ne(s "n$ r&mors
#et t!e %nsi$er ;e(s 1ree e"c! morning/
Related ideos
AntiAlias ode 0 speci1ies t!e "nti"li"sing level bet(een 9o(, ?e$i&m "n$ 4ig!/ 8!is intern"ll+ (ill set t!e d!extAlp"a#its "n$ d$rap"icsAlp"a#its 'host(cript s(itc!es to "ppropri"te v"l&es/ %pi 0 $ots per inc!/ %ntern"ll+ sets t!e r s(itc!/ 8!is propert+ is not &se$ i1 " p"per siHe is set/ $rid&it ode 0 controls t!e te<t re"$"bilit+ mo$e/ %ntern"ll+ sets t!e d$rid&it!! s(itc!/ Image&ormat 0 speci1ies t!e o&tp&t im"ge 1orm"t/ %ntern"ll+ sets t!e s%E'ICE s(itc!/ PaperSi(e 0 speci1ies " p"per siHe 1rom one o1 t!e st"n$"r$ siHes s&pporte$ b+ 'host(cript/ !rim ode 0 speci1ies !o( t!e im"ge s!o&l$ be siHe$/ Co&r mile"ge m"+ v"r+ i1 +o& tr+ "n$ &se t!e p"per siHe option/ %ntern"ll+ sets eit!er t!e d&I)E% E%IA "n$ sPAPERSI*E or t!e d+seCrop#ox or t!e d+se!rim#ox s(itc!es/
8+pic"l settings co&l$ loo. li.e t!is,
oll"pse J op+ o$e Pdf2ImageSettings settings, settings - new Pdf2ImageSettings./, settings.AntiAlias ode - AntiAlias ode.0ig", settings.%pi - 122, settings.$rid&it ode - $rid&it ode.!opological, settings.Image&ormat - Image&ormat.Png23, settings.!rim ode - Pdf!rim ode.Crop#ox,
Related Articles
W56 :is&"l 5rint omponent Esing t!e Web'ro(ser control, simpli1ie$ )eport #ener"tor Esing " Wi.i 1or .no(le$ge s!"ring "n$ SF9 Server $"t"b"se $oc&ment"tion >pening - S"ving m<iple t+pes o1 $oc&ments in ?6 ?D% "pplic"tions F&ic. 7 Doc&ment"tion &sing G?9 D+n"mic 5"gin"tion #ener"te 8!&mbn"il %m"ges 1rom 5D6 Doc&ments Step %n 8o, S!"re5oint 2013 >nline 0 Sites * ontents
10 ?in&tes to $oc&ment +o&r co$e Wor.ing (it! r+st"l )eports in /;et ?>SS ?<iling&"l Site on1ig&r"tion #ener"ting D+n"mic 5D6 Doc&ments &sing t!e >pen So&rce Scr+ber 9ibr"r+ 8!e Eltim"te 8oolbo< 3D 8"b :ie(s 5D6 6ile An"l+Her Wit! 7 5"rsing l"sses oll"pse J op+ o$e re"ting +o&r 1irst ?6 Doc-:ie( "pplic"tion G?9 cl"ss 1or processing "n$ b&il$ing simple G?9 $oc&ments 'eginnerAs #&i$e, I<ploring %%S 6/0 Wit! AS5/;I8 '&il$ing " Web ?ess"ge 'o"r$ &sing :is&"l St&$io 2008 5"rt %% 0 5osting mess"ges &sing ?icroso1t Wor$ Driver Development 5"rt 3, %ntro$&ction to $river conte<ts AS5/;I8 Applic"tion "n$ 5"ge
ConvertPdfPage!oImage 0 converts " given p"ge in t!e 5D6 into "n im"ge (!ic! is s"ve$ to $is. $etImage 0 converts " p"ge in t!e 5D6 into "n im"ge "n$ ret&rns t!e im"ge $etImages 0 converts " r"nge o1 pages into t!e 5D6 into im"ges "n$ ret&rns "n im"ge "rr"+ PageCount 0 ret&rns t!e n&mber o1 pages in t!e so&rce 5D6 Pdf&ilename 0 ret&rns or sets t!e 1ilen"me o1 t!e 5D6 document to convert PdfPassword 0 ret&rns or sets t!e p"ss(or$ o1 t!e 5D6 document to convert Settings 0 ret&rns or sets t!e settings obBect $escribe$ "bove
A t+pic"l e<"mple to convert t!e 1irst im"ge in " 5D6 document,
' of $
#/'(/'%#) (:'$ P*
http://www.codeproject.com/ rtic!es/"#$$%%/Convert&a...
9i1e +cle arguments - new %ictionary5GhostScriptCommand6 o78ect9./, // basic GhostScript setup arguments.Add.GhostScriptCommand.Silent6 null/, arguments.Add.GhostScriptCommand.Safer6 null/, arguments.Add.GhostScriptCommand.#atc"6 null/, arguments.Add.GhostScriptCommand.:oPause6 null/, // specify the output arguments.Add.GhostScriptCommand.%evice6 GhostScriptAPI.$et%evice:ame.settings.Image&ormat//, arguments.Add.GhostScriptCommand.<utput&ile6 outputImage&ile:ame/, // page numbers arguments.Add.GhostScriptCommand.&irstPage6 page:um7er/, arguments.Add.GhostScriptCommand.=astPage6 page:um7er/, // graphics options arguments.Add.GhostScriptCommand.+seCIEColor6 null/, if .settings.AntiAlias ode >- AntiAlias ode.:one/ ; arguments.Add.GhostScriptCommand.!extAlp"a#its6 settings.AntiAlias ode/, arguments.Add.GhostScriptCommand.$rap"icsAlp"a#its6 settings.AntiAlias ode/, ? arguments.Add.GhostScriptCommand.$rid!o&it!!6 settings.$rid&it ode/, // image size if .settings.!rim ode >- Pdf!rim ode.PaperSi(e/ arguments.Add.GhostScriptCommand.@esolution6 settings.%pi.!oString.//, switc" .settings.!rim ode/ ; case Pdf!rim ode.PaperSi(eA if .settings.PaperSi(e >- PaperSi(e.%efault/ ; arguments.Add.GhostScriptCommand.&ixed edia6 true/, arguments.Add.GhostScriptCommand.PaperSi(e6 settings.PaperSi(e/, ? 7reak, case Pdf!rim ode.!rim#oxA arguments.Add.GhostScriptCommand.+se!rim#ox6 true/, 7reak, case Pdf!rim ode.Crop#oxA arguments.Add.GhostScriptCommand.+seCrop#ox6 true/, 7reak, ? // pdf password if .>string.Is:ull<rEmpty.password// arguments.Add.GhostScriptCommand.P%&Password6 password/, // pdf filename arguments.Add.GhostScriptCommand.Input&ile6 pdf&ile:ame/, return arguments, 8en 8ips o1 Web App 8esting
Related Researc!
9e"rn Agile, 8en 8ips 1or 9"&nc!ing "n$ 8esting 4ig! F&"lit+ Apps 1or t!e Americ"n ?"r.et
8!e Issenti"l #&i$e to ?obile App 8esting, 8ips 1or Developers in ESA * "n"$"
As +o& c"n see 1rom t!e met!o$ "bove, t!e comm"n$s "re being ret&rne$ "s " strongl+ t+pe$ $iction"r+ 0 t!e GhostScriptAPI cl"ss (ill convert t!ese into t!e correct 'host(cript comm"n$s, b&t t!e enum is m&c! e"sier to (or. (it! 1rom +o&r co$eL 8!e 1ollo(ing is "n e<"mple o1 t!e t+pic"l 'host(cript comm"n$s to convert " single p"ge in " 5D6 document,
oll"pse J op+ o$e BC BdSA&E@ Bd#A!C0 Bd:<PA+SE Bs%E'ICE-pngDEm Bs<utput&ile-tmpFG#C.tmp Bd&irstPage-D Bd=astPage-D Bd+seCIEColor Bd!extAlp"a#its-3 Bd$rap"icsAlp"a#its-3 Bd$rid&it!!-2 BrDH2 Bd+seCrop#ox-true sample.pdf
8!e ne<t step is to c"ll 'host(cript "n$ convert t!e 5D6 (!ic! is $one using t!e ConvertPdfPage!oImage met!o$,
oll"pse J op+ o$e pu7lic void ConvertPdfPage!oImage.string output&ile:ame6 int page:um7er/ ; if .page:um7er 5 D II page:um7er 9 t"is.PageCount/ t"row new ArgumentException.4Page num7er is out of 7ounds46 4page:um7er4/, using .GhostScriptAPI api - new GhostScriptAPI.// api.Execute.t"is.$etConversionArguments.t"is._pdf&ile:ame6 output&ile:ame6 page:um7er6 t"is.PdfPassword6 t"is.Settings//, ?
As +o& c"n see, t!is is " ver+ simple c"ll 0 create "n inst"nce o1 t!e GhostScriptAPI cl"ss "n$ t!en p"ss in t!e list o1 p"r"meters to e<ec&te/ 8!e GhostScriptAPI cl"ss t".es c"re o1 ever+t!ing else/ >nce t!e 1ile is s"ve$ to $is., +o& c"n t!en lo"$ it into " #itmap or Image obBect 1or &se in +o&r "pplic"tion/ DonAt
" of $
#/'(/'%#) (:'$ P*
http://www.codeproject.com/ rtic!es/"#$$%%/Convert&a...
1orget to $elete t!e 1ile (!en +o& "re 1inis!e$ (it! itL Altern"tivel+, t!e $etImage met!o$ (ill convert t!e 1ile "n$ ret&rn t!e bitm"p im"ge 1or +o&, "&tom"tic"ll+ $eleting t!e tempor"r+ 1ile/ 8!is s"ves +o& 1rom !"ving to (orr+ "bo&t provi$ing "n$ $eleting t!e o&tp&t 1ile, b&t it $oes me"n +o& "re responsible 1or $isposing o1 t!e ret&rne$ bitm"p/
oll"pse J op+ o$e pu7lic #itmap $etImage.int page:um7er/ ; #itmap result, string work&ile, if .page:um7er 5 D II page:um7er 9 t"is.PageCount/ t"row new ArgumentException.4Page num7er is out of 7ounds46 4page:um7er4/, work&ile - Pat".$et!emp&ile:ame./, try ; t"is.ConvertPdfPage!oImage.work&ile6 page:um7er/, using .&ileStream stream - new &ileStream.work&ile6 &ile ode.<pen6 &ileAccess.@ead// result - new #itmap.stream/, ? finally ; &ile.%elete.work&ile/, ? return result, ?
Co& co&l$ "lso convert " r"nge o1 pages "t once using t!e $etImages met!o$,
oll"pse J op+ o$e pu7lic #itmapJK $etImages.int startPage6 int lastPage/ ; =ist5#itmap9 results, if .startPage 5 D II startPage 9 t"is.PageCount/ t"row new ArgumentException .4Start page num7er is out of 7ounds46 4startPage4/, if .lastPage 5 D II lastPage 9 t"is.PageCount/ t"row new ArgumentException .4=ast page num7er is out of 7ounds46 4lastPage4/, else if .lastPage 5 startPage/ t"row new ArgumentException .4=ast page cannot 7e less t"an start page46 4lastPage4/, results - new =ist5#itmap9./, for .int i - startPage, i 5- lastPage, iLL/ results.Add.t"is.$etImage.i//, return results.!oArray./, ?
4n Conclusion
8!e "bove met!o$s provi$e " simple ("+ o1 provi$ing b"sic 5D6 vie(ing in +o&r "pplic"tions/ %n t!e ne<t p"rt o1 t!is series, (e $escribe !o( to e<ten$ t!e %m"ge'o< component to s&pport conversion "n$ n"vig"tion/
"icense
8!is "rticle, "long (it! "n+ "ssoci"te$ so&rce co$e "n$ 1iles, is license$ &n$er 8!e o$e 5roBect >pen 9icense ( 5>9)
) of $
#/'(/'%#) (:'$ P*
http://www.codeproject.com/ rtic!es/"#$$%%/Convert&a...
Article 8op
*em8er 93"!&9!
9.,o2.!" 3:!"
4i, 4o( c"n % convert m<iple p"ge p$1 1ile to ti11 !"s sinle long p"ge/ 8!"n.s/
Sign %n M :ie( 8!re"$ M 5erm"lin.
70;ov013 2,N2
Well, i1 +o& !"ve converte$ t!e 5D6 into " series o1 im"ges, +o& c"n @&er+ t!eir Si(e properties to $etermine t!e 1in"l siHe o1 t!e im"ge, cre"te " ne( #itmap obBect "n$ t!en &se t!e met!o$s o1 t!e $rap"ics cl"ss to $r"( t!e $i11erent im"ges "ppropri"tel+ into t!e 1in"l im"ge/ As /;I8 s&pports re"$ing "n$ (riting 8%66 1iles (not too s&re "bo&t m<i0p"ge 8%66 1iles t!o&g!) +o& (o&l$ t!en be "ble to s"ve +o&r ne( im"ge into t!"t 1orm"t/ Seems prett+ str"ig!t1or("r$ to me/// )eg"r$sO )ic!"r$ ?oss
Sign %n M :ie( 8!re"$ M 5erm"lin.
*em8er !002%"%0
;.Oct.!" 2!:23
4i % "m (or.ing in (eb "pplic"tion "n$ i !"ve to convert " 5D6 into im"ge t!ere/ %s t!is "lso (or.ing 1or (eb "pplic"tion "lso 2
Sign %n M :ie( 8!re"$ M 5erm"lin.
100>ct013 7,58
Ces, t!is (or.s (it! AS5/net "pplic"tions ((it! " c"ve"t or 10) 0 +o& c"n see " 1ollo( &p "rticle % $i$ $escribing m+ e<periences (it! t!is !ere/ 4ope t!"t !elpsL )eg"r$sO )ic!"r$ ?oss
Sign %n M :ie( 8!re"$ M 5erm"lin.
*em8er !009$""0
2$.Aug.!" !%:&"
+ of $
#/'(/'%#) (:'$ P*
http://www.codeproject.com/ rtic!es/"#$$%%/Convert&a...
!ttps,--g!ostscriptnet/co$eple</comPQR %t "llo(s +o& to ren$er p$1 p"ges to im"ges in memor+ so +o& $ont nee$ to s"ve it on t!e $is. 1irst/ %t !"s " lot more 1e"t&res///
Sign %n M :ie( 8!re"$ M 5erm"lin.
*em8er !0!%3%!3
$.Aug.!" 20:""
!i, $o(nlo"$e$ &r co$e, compile bo&t! /$ll0s/ compile is o./ "$$ t!em to m+ proBect/ (!en tr+ to &se,
c"tc! e<ception 0 6"ile$ to process #!ostScript comm"n$/ (!"t c"n it be2 % !"$ no gs32$ll/$ll on m+ 5 "t t!e beginning/ W!en % trie$ to &se
% !"$ e<ception somet!ing li.e t!"t 0 c"nnot 1o&n$ gs32$ll mo$&le/// % 1o&n$, $o(nlo"$e$ "n$ tr+ pl"ce$ t!"t $ll into Win$o(sSs+stem32 "n$ into Win$o(sSs+s(o(6N/ 8!ere is no e11ect e<cept % got ne( .in$ o1 e<pection mentione$ "bove/ (!"t "$$itio"nl in1orm"tion & nee$ to "ns(er2 3333333333333333333333333333333333333333333333333333333333333333333333333333 solve$L ?ention plH in &r "cticle t!"t &ser nee$ last version o1 #!ostScript /$ll in &ser pro=ect 8in.folder, not (!ere elseL
modified 8-Aug- ! "#$%am.
torti;"
% trie$ to m".e " b"tc! o1 converting p$1s in sever"l Bpgs/ &n1ort&n"tel+, % got "n o&t o1 memor+ e<ception "1ter N8 1iles/ is t!ere " b&g in t!e sol&tion or is g!ostscript not possible to $o t!"t2 t!"n.s, t!orsten
Sign %n M :ie( 8!re"$ M 5erm"lin.
280=&n013 5,N5
W!ere $oes t!e o&t o1 memor+ e<ception to come 1rom2 %A$ be more incline$ to "ss&me t!e error c"me 1rom !o( it ("s being &se$/ 6or e<"mple, +o& s"i$ N8 1iles 0 !o( m"n+ im"ges (ere in t!ese 1iles "n$ (ere +o& $isposing o1 t!e im"ge 1iles once +o&A$ 1inis!e$ (it! t!em2 )eg"r$sO )ic!"r$ ?oss
Sign %n M :ie( 8!re"$ M 5erm"lin.
m"tri<37
180=&l013 23,52
8!e "pp spi.es 1rom N0mb to D00mb (!en % c"ll t!e #et%m"ges co$e/ %Am B&st testing "g"inst " rel"tivel+ sm"ll p$1 1ile/ #r"nte$, %Am setting t!e @&"lit+ to " !ig!er v"l&e, b&t "1ter t!e processing is $one, t!e memor+ is never
, of $
#/'(/'%#) (:'$ P*
http://www.codeproject.com/ rtic!es/"#$$%%/Convert&a...
rele"se$ even i1 i e<plicitl+ .ill "ll t!e v"l&es/ %1 % r&n t!e process t!e 2n$ time, it gives me t!e o&t o1 memor+ e<ception/ %s t!ere somet!ing else % nee$ to $o to 1ree &p t!e reso&rces "1ter t!e processing is $one2
Sign %n M :ie( 8!re"$ M 5erm"lin.
220=&l013 6,36
Are +o& c"lling %ispose on e"c! #itmap obBect "1ter +o&Are 1inis!e$ (it! it2 % s&ppose itAs possible t!e #"rb"ge ollector isnAt cle"ring &p 1"st eno&g!, +o& co&l$ tr+ intro$&cing " $C.Collect c"ll or t(o, "lt!o&g! t!is (ill "11ect t!e per1orm"nce o1 +o&r "pplic"tion "n$ slo( it $o(n " to&c!/
Sign %n M :ie( 8!re"$ M 5erm"lin.
)e1res!
! 2 ;e<t
#ener"l
;e(s
S&ggestion
F&estion
'&g
Ans(er
=o.e
)"nt
A$min
5erm"lin. J A$vertise J 5riv"c+ J ?obile Web02 J 2/8/1N0127/2 J 9"st Ep$"te$ 21 =&n 2013
Article op+rig!t 2012 b+ )ic!"r$ ="mes ?oss Iver+t!ing else op+rig!t T o$e5roBect, 1DDD0201N 8erms o1 Ese
$ of $
#/'(/'%#) (:'$ P*