pdf文件显示由于项目需要在.net下将pdf转换为普通图像格式,在网上搜了好久终于找到一个解决方案,于是采用拿来主义直接用。来源见代码中注释,感谢原作者。
[c-sharp] view plaincopyPRint?usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingSystem.Collections;/**ConvertPDFtoImageFormat(JPEG)usingGhostscriptAPIconvertapdftojpegusingghostscriptcommandline:gswin32c-q-dQUIET-dPARANOIDSAFER-dBATCH-dNOPAUSE-dNOPROMPT-dMaxBitmap=500000000-dFirstPage=1-dAlignToPixels=0-dGridFitTT=0-sDEVICE=jpeg-dTextAlphaBits=4-dGraphicsAlphaBits=4-r100x100-sOutputFile=output.jpgtest.pdfseealso:http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/and:http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspxNote:copygsdll32.dlltosystem32directorybeforeusingthisghostscriptwrapper.**/namespaceConvertPDF{///<summary>//////ClasstoconvertapdftoanimageusingGhostScriptDLL///Creditforthiscodegoto:RangelAvulso///ionlyfixalittlebugandrefactoralittle///http://www.hrangel.com.br/index.php/2006/12/04/converter-pdf-para-imagem-jpeg-em-c////</summary>///<seealsocref="http://www.hrangel.com.br/index.php/2006/12/04/converter-pdf-para-imagem-jpeg-em-c/"/>classPDFConvert{#regionGhostScriptImport///<summary>CreateanewinstanceofGhostscript.Thisinstanceispassedtomostothergsapifunctions.Thecaller_handlewillbeprovidedtocallbackfunctions.///Atthisstage,Ghostscriptsupportsonlyoneinstance.</summary>///<paramname="pinstance"></param>///<paramname="caller_handle"></param>///<returns></returns>[DllImport("gsdll32.dll",EntryPoint="gsapi_new_instance")]privatestaticexternintgsapi_new_instance(outIntPtrpinstance,IntPtrcaller_handle);///<summary>Thisistheimportantfunctionthatwillperformtheconversion</summary>///<paramname="instance"></param>///<paramname="argc"></param>///<paramname="argv"></param>///<returns></returns>[DllImport("gsdll32.dll",EntryPoint="gsapi_init_with_args")]privatestaticexternintgsapi_init_with_args(IntPtrinstance,intargc,IntPtrargv);///<summary>///Exittheinterpreter.Thismustbecalledonshutdownifgsapi_init_with_args()hasbeencalled,andjustbeforegsapi_delete_instance().///</summary>///<paramname="instance"></param>///<returns></returns>[DllImport("gsdll32.dll",EntryPoint="gsapi_exit")]privatestaticexternintgsapi_exit(IntPtrinstance);///<summary>///DestroyaninstanceofGhostscript.Beforeyoucallthis,Ghostscriptmusthavefinished.IfGhostscripthasbeeninitialised,youmustcallgsapi_exitbeforegsapi_delete_instance.///</summary>///<paramname="instance"></param>[DllImport("gsdll32.dll",EntryPoint="gsapi_delete_instance")]privatestaticexternvoidgsapi_delete_instance(IntPtrinstance);#endregion#regionVariablesprivatestring_sDeviceFormat;privateint_iWidth;privateint_iHeight;privateint_iResolutionX;privateint_iResolutionY;privateint_iJPEGQuality;privateBoolean_bFitPage;privateIntPtr_objHandle;#endregion#regionProprietiespublicstringOutputFormat{get{return_sDeviceFormat;}set{_sDeviceFormat=value;}}publicintWidth{get{return_iWidth;}set{_iWidth=value;}}publicintHeight{get{return_iHeight;}set{_iHeight=value;}}publicintResolutionX{get{return_iResolutionX;}set{_iResolutionX=value;}}publicintResolutionY{get{return_iResolutionY;}set{_iResolutionY=value;}}publicBooleanFitPage{get{return_bFitPage;}set{_bFitPage=value;}}///<summary>QualityofcompressionofJPG</summary>publicintJPEGQuality{get{return_iJPEGQuality;}set{_iJPEGQuality=value;}}#endregion#regionInitpublicPDFConvert(IntPtrobjHandle){_objHandle=objHandle;}publicPDFConvert(){_objHandle=IntPtr.Zero;}#endregionprivatebyte[]StringToAnsiZ(stringstr){//'ConvertaUnicodestringtoanullterminatedAnsistringforGhostscript.//'Theresultisstoredinabytearray.Lateryouwillneedtoconvert//'thisbytearraytoapointerwithGCHandle.Alloc(XXXX,GCHandleType.Pinned)//'andGSHandle.AddrOfPinnedObject()intintElementCount;intintCounter;byte[]aAnsi;bytebChar;intElementCount=str.Length;aAnsi=newbyte[intElementCount+1];for(intCounter=0;intCounter<intElementCount;intCounter++){bChar=(byte)str[intCounter];aAnsi[intCounter]=bChar;}aAnsi[intElementCount]=0;returnaAnsi;}///<summary>Convertthefile!</summary>publicvoidConvert(stringinputFile,stringoutputFile,intfirstPage,intlastPage,stringdeviceFormat,intwidth,intheight){//Avoidtoworkwhenthefiledoesn'texistif(!System.IO.File.Exists(inputFile)){System.Windows.Forms.MessageBox.Show(string.Format("Thefile:'{0}'doesn'texist",inputFile));return;}intintReturn;IntPtrintGSInstanceHandle;object[]aAnsiArgs;IntPtr[]aPtrArgs;GCHandle[]aGCHandle;intintCounter;intintElementCount;IntPtrcallerHandle;GCHandlegchandleArgs;IntPtrintptrArgs;string[]sArgs=GetGeneratedArgs(inputFile,outputFile,firstPage,lastPage,deviceFormat,width,height);//ConverttheUnicodestringstonullterminatedANSIbytearrays//thengetpointerstothebytearrays.intElementCount=sArgs.Length;aAnsiArgs=newobject[intElementCount];aPtrArgs=newIntPtr[intElementCount];