首页 > 学院 > 开发设计 > 正文

pdf文件显示

2019-11-17 03:16:29
字体:
来源:转载
供稿:网友

pdf文件显示

由于项目需要在.net下将pdf转换为普通图像格式,在网上搜了好久终于找到一个解决方案,于是采用拿来主义直接用。来源见代码中注释,感谢原作者。

[c-sharp] view plaincopyPRint?
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Text;
  4. usingSystem.Runtime.InteropServices;
  5. usingSystem.Collections;
  6. /**
  7. ConvertPDFtoImageFormat(JPEG)usingGhostscriptAPI
  8. convertapdftojpegusingghostscriptcommandline:
  9. 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.pdf
  10. seealso:http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/
  11. and:http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx
  12. Note:copygsdll32.dlltosystem32directorybeforeusingthisghostscriptwrapper.
  13. *
  14. */
  15. namespaceConvertPDF
  16. {
  17. ///<summary>
  18. ///
  19. ///ClasstoconvertapdftoanimageusingGhostScriptDLL
  20. ///Creditforthiscodegoto:RangelAvulso
  21. ///ionlyfixalittlebugandrefactoralittle
  22. ///http://www.hrangel.com.br/index.php/2006/12/04/converter-pdf-para-imagem-jpeg-em-c/
  23. ///</summary>
  24. ///<seealsocref="http://www.hrangel.com.br/index.php/2006/12/04/converter-pdf-para-imagem-jpeg-em-c/"/>
  25. classPDFConvert
  26. {
  27. #regionGhostScriptImport
  28. ///<summary>CreateanewinstanceofGhostscript.Thisinstanceispassedtomostothergsapifunctions.Thecaller_handlewillbeprovidedtocallbackfunctions.
  29. ///Atthisstage,Ghostscriptsupportsonlyoneinstance.</summary>
  30. ///<paramname="pinstance"></param>
  31. ///<paramname="caller_handle"></param>
  32. ///<returns></returns>
  33. [DllImport("gsdll32.dll",EntryPoint="gsapi_new_instance")]
  34. privatestaticexternintgsapi_new_instance(outIntPtrpinstance,IntPtrcaller_handle);
  35. ///<summary>Thisistheimportantfunctionthatwillperformtheconversion</summary>
  36. ///<paramname="instance"></param>
  37. ///<paramname="argc"></param>
  38. ///<paramname="argv"></param>
  39. ///<returns></returns>
  40. [DllImport("gsdll32.dll",EntryPoint="gsapi_init_with_args")]
  41. privatestaticexternintgsapi_init_with_args(IntPtrinstance,intargc,IntPtrargv);
  42. ///<summary>
  43. ///Exittheinterpreter.Thismustbecalledonshutdownifgsapi_init_with_args()hasbeencalled,andjustbeforegsapi_delete_instance().
  44. ///</summary>
  45. ///<paramname="instance"></param>
  46. ///<returns></returns>
  47. [DllImport("gsdll32.dll",EntryPoint="gsapi_exit")]
  48. privatestaticexternintgsapi_exit(IntPtrinstance);
  49. ///<summary>
  50. ///DestroyaninstanceofGhostscript.Beforeyoucallthis,Ghostscriptmusthavefinished.IfGhostscripthasbeeninitialised,youmustcallgsapi_exitbeforegsapi_delete_instance.
  51. ///</summary>
  52. ///<paramname="instance"></param>
  53. [DllImport("gsdll32.dll",EntryPoint="gsapi_delete_instance")]
  54. privatestaticexternvoidgsapi_delete_instance(IntPtrinstance);
  55. #endregion
  56. #regionVariables
  57. privatestring_sDeviceFormat;
  58. privateint_iWidth;
  59. privateint_iHeight;
  60. privateint_iResolutionX;
  61. privateint_iResolutionY;
  62. privateint_iJPEGQuality;
  63. privateBoolean_bFitPage;
  64. privateIntPtr_objHandle;
  65. #endregion
  66. #regionProprieties
  67. publicstringOutputFormat
  68. {
  69. get{return_sDeviceFormat;}
  70. set{_sDeviceFormat=value;}
  71. }
  72. publicintWidth
  73. {
  74. get{return_iWidth;}
  75. set{_iWidth=value;}
  76. }
  77. publicintHeight
  78. {
  79. get{return_iHeight;}
  80. set{_iHeight=value;}
  81. }
  82. publicintResolutionX
  83. {
  84. get{return_iResolutionX;}
  85. set{_iResolutionX=value;}
  86. }
  87. publicintResolutionY
  88. {
  89. get{return_iResolutionY;}
  90. set{_iResolutionY=value;}
  91. }
  92. publicBooleanFitPage
  93. {
  94. get{return_bFitPage;}
  95. set{_bFitPage=value;}
  96. }
  97. ///<summary>QualityofcompressionofJPG</summary>
  98. publicintJPEGQuality
  99. {
  100. get{return_iJPEGQuality;}
  101. set{_iJPEGQuality=value;}
  102. }
  103. #endregion
  104. #regionInit
  105. publicPDFConvert(IntPtrobjHandle)
  106. {
  107. _objHandle=objHandle;
  108. }
  109. publicPDFConvert()
  110. {
  111. _objHandle=IntPtr.Zero;
  112. }
  113. #endregion
  114. privatebyte[]StringToAnsiZ(stringstr)
  115. {
  116. //'ConvertaUnicodestringtoanullterminatedAnsistringforGhostscript.
  117. //'Theresultisstoredinabytearray.Lateryouwillneedtoconvert
  118. //'thisbytearraytoapointerwithGCHandle.Alloc(XXXX,GCHandleType.Pinned)
  119. //'andGSHandle.AddrOfPinnedObject()
  120. intintElementCount;
  121. intintCounter;
  122. byte[]aAnsi;
  123. bytebChar;
  124. intElementCount=str.Length;
  125. aAnsi=newbyte[intElementCount+1];
  126. for(intCounter=0;intCounter<intElementCount;intCounter++)
  127. {
  128. bChar=(byte)str[intCounter];
  129. aAnsi[intCounter]=bChar;
  130. }
  131. aAnsi[intElementCount]=0;
  132. returnaAnsi;
  133. }
  134. ///<summary>Convertthefile!</summary>
  135. publicvoidConvert(stringinputFile,stringoutputFile,
  136. intfirstPage,intlastPage,stringdeviceFormat,intwidth,intheight)
  137. {
  138. //Avoidtoworkwhenthefiledoesn'texist
  139. if(!System.IO.File.Exists(inputFile))
  140. {
  141. System.Windows.Forms.MessageBox.Show(string.Format("Thefile:'{0}'doesn'texist",inputFile));
  142. return;
  143. }
  144. intintReturn;
  145. IntPtrintGSInstanceHandle;
  146. object[]aAnsiArgs;
  147. IntPtr[]aPtrArgs;
  148. GCHandle[]aGCHandle;
  149. intintCounter;
  150. intintElementCount;
  151. IntPtrcallerHandle;
  152. GCHandlegchandleArgs;
  153. IntPtrintptrArgs;
  154. string[]sArgs=GetGeneratedArgs(inputFile,outputFile,
  155. firstPage,lastPage,deviceFormat,width,height);
  156. //ConverttheUnicodestringstonullterminatedANSIbytearrays
  157. //thengetpointerstothebytearrays.
  158. intElementCount=sArgs.Length;
  159. aAnsiArgs=newobject[intElementCount];
  160. aPtrArgs=newIntPtr[intElementCount];<
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表