• 首页
  • 栏目
  • CRM
  • Delphi文件正文提取开发组件--文件内容搜索的高效工具

Delphi文件正文提取开发组件--文件内容搜索的高效工具

  • 2022-01-23
  • Admin

Graccvs文件正文提取开发组件支持各种文件提取正文,为Lucene/CLucene, Elasticsearch, Sphinx等全文检索工具,为OA,ERP,CRM系统使用文件提供文件正文使用和搜索,支持常见各种文件格式”.pdf", ".doc", ".odt", ".docx", ".dotm", ".docm", ".wps", ".xls", ".xlsx", ".xlsm", ".xltm", ".et", ".ppt", ".pptx", ".potm", ".pptm", ".ppsm", ".dps", ".ofd"(电子发票版式文件), ".rtf",".html", ".htm", ".mht", ".mhtml", ".eml", ".emmx", "xmind", "gmind", ".chm", ".zip" 等。

Graccvs提供免费版本,以下是调用过程及代码,起来文件输入修改为要提取的文件名称和类型即可。

以下是使用DLL过程:

1:建立Console Application工程或者 VCL Application工程。
2:拷贝 graccvs64.dll到工程中, 默认在exe输出位置。
3:拷贝graccvsTest.dpr文件相关代码 。
4:编译运行工程 。
5:点击这里下载完整工程包,这里下载最新DLL文件

graccvsTest.dpr :

  1. program graccvsTest;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils,
  6. Windows,
  7. Types,
  8. DateUtils,
  9. Classes;
  10. type
  11. // ----------------------以下为函数说明----------------------
  12. // DLL加载并设置动态库需要的临时文件夹,且对此文件夹要有读写权限
  13. TLoad = procedure(TempDir: PRawByteString); cdecl;
  14. (*
  15. 注册软件:
  16. 方式1:输入参数,corp为公司名称licText为注册码
  17. 方式2:把授权文件grauth.lic保存到动态库相同文件夹,调用TAuth函数(corp, licText都为空)系统自动加载grauth.lic
  18. 返回值:
  19. 0:免费版本许可为空
  20. 1:许可正常
  21. 2:序列号(公司授权名称)错误
  22. 3:加密数据格式错误
  23. 4:许可过期
  24. 5:许可验证错误
  25. 6:无效的许可
  26. 7:未知错误
  27. 注1:如果注册失败,系统变为免费版本
  28. 注2:免费版也需要调用此函数,corp和licText都为空
  29. *)
  30. TAuth = function(Corp, LicText: PRawByteString): Cardinal; cdecl;
  31. // 释放TToString, THttpToString等函数的返回指针
  32. TFreeString = procedure(P: PRawByteString); cdecl;
  33. // 得到最后的错误信息
  34. TLastError = function(): PRawByteString; cdecl;
  35. // 提供文件正文
  36. // Infile输入文件地址, 返回UTF-8编码字符串数据指针(此指针需要使用TFreeString函数释放内存)
  37. TToString = function(Infile: PRawByteString): PRawByteString; cdecl;
  38. // 提供文件正文,并保存到目标文件
  39. // Infile输入文件地址, Outfile为TXT目标文件文件地址
  40. TToTextFile = function(Infile, Outfile: PRawByteString): Cardinal; cdecl;
  41. // 提取Http/Https文件,返回字符串数据指针
  42. // Url=Http/Https地址
  43. // FileExt=文件类型(比如:".pdf"),
  44. // Timeout=超时设置,超过此数值系统终止下载文件。单位为毫秒,默认为0(等待文件下载直到完成)
  45. // HttpParams=JSON格式Header数据和Cookie数据,默认为空
  46. (*
  47. JSON格式如下:
  48. {"headers":
  49. [{名称1: 值1},{名称2: 值2},...],
  50. "cookies":[
  51. {"name": 名称(字符串), "value": 值(字符串), "expires": 有效期(整数,单位毫秒),
  52. "path": 路径(字符串), "domain": 域名(字符串)},
  53. {"name": 名称(字符串), "value": 值(字符串), "expires": 有效期(整数,单位毫秒),
  54. "path": 路径(字符串), "domain": 域名(字符串)}
  55. ...
  56. ]}
  57. 例如:
  58. {"headers":[{"client_id": "g01x9"}, {"client_secret": "e23c89cc9fe"}, {"client_index": 10092}],
  59. "cookies":[{"name": "ga", "value": "1020", "expires":36000000, "path": "/hx/", "domain":"www.gaya-soft.cn"},
  60. {"name": "xc3", "value": "10099", "expires":240000, "path": "", "domain":""}]}
  61. *)
  62. // 返回UTF-8编码字符串数据指针(此指针需要使用TFreeString函数释放内存)
  63. THttpToString = function(Url, FileExt: PRawByteString; Timeout: Cardinal; HttpParams: PRawByteString)
  64. : PRawByteString; cdecl;
  65. // 下载Http文件,并提取文本,保存到目标文件
  66. // Outfile为TXT目标文件文件地址,其他参数和THttpToString参数相同
  67. THttpToTextFile = function(Url, FileExt, Outfile: PRawByteString; Timeout: Cardinal; HttpParams: PRawByteString)
  68. : Cardinal; cdecl;
  69. // 文件提取异步任务, Infile输入文件地址, Outfile为TXT目标文件文件
  70. // 如果提取某个文件错误,则 Outfile的内容如下格式: @ErrCode:错误代码, ErrMessage:错误提示
  71. TAddTask = procedure(InFilePtr, OutTxtFilePtr: PRawByteString); cdecl;
  72. // 异步提取Http文件任务,参数同 THttpToTextFile 函数
  73. TAddHttpTask = procedure(Url, FileExt, OutTxtFile: PRawByteString; Timeout: Cardinal;
  74. Params: PRawByteString); cdecl;
  75. // 开始执行异步任务,返回值=1开始执行, 其他值未识别
  76. // =2 免费版不支持此功能,=3 没有可以执行的任务 ,=4 当前任务未完成
  77. TAsyncStart = function(): Cardinal; cdecl;
  78. // 停止任务
  79. TAsyncStop = procedure(); cdecl;
  80. // 一直等待,直到全部异步任务结束
  81. TAsyncWait = procedure(); cdecl;
  82. // 得到执行异步任务的状态, =0 没开始, =1 正在处理中,=2 已中断, =99 处理完成
  83. TAsyncState = function(): Cardinal; cdecl;
  84. // 设置执行异步任务的并发数量(不大于软件授权数量),返回并发数量
  85. TAsyncMaxProcs = function(Num: Cardinal): Cardinal;
  86. // 关闭动态库前调用此函数释放资源,否则关闭DLL会发生错误
  87. TUnload = procedure(); cdecl;
  88. // 软件注册序列号,用此序列号申请注册码。每次调用返回的值是不一样的
  89. // 返回UTF-8编码字符串数据指针(此指针需要使用TFreeString函数释放内存)
  90. TUuid = function(): PRawByteString; cdecl;
  91. // 提取文本的错误类型
  92. const
  93. TFE_OK = 0;
  94. TFE_UNKNOW = 1;
  95. TFE_FILE_NOTEXIST = 2;
  96. TFE_SAVE_ERROR = 3;
  97. TFE_OUTSIZE = 4;
  98. TFE_UNSUPPORTED = 5;
  99. TFE_ERROR_INTERFACE = 6;
  100. TFE_HTTP_ERR = 7;
  101. TFE_HTTP_FILE_NULL = 8;
  102. TFE_LICENCE_ERR = 9;
  103. var
  104. PLib: DWORD;
  105. FToTextFile: TToTextFile;
  106. FToString: TToString;
  107. FHttpToTextFile: THttpToTextFile;
  108. FHttpToString: THttpToString;
  109. FFreeString: TFreeString;
  110. FLastError: TLastError;
  111. function DllInit(): Boolean;
  112. var
  113. P2, P3, P4, P5, P6, P7: Pointer;
  114. begin
  115. P2 := GetProcAddress(PLib, 'LastErr');
  116. P3 := GetProcAddress(PLib, 'ToTextFile');
  117. P4 := GetProcAddress(PLib, 'ToString');
  118. P5 := GetProcAddress(PLib, 'HttpToTextFile');
  119. P6 := GetProcAddress(PLib, 'HttpToString');
  120. P7 := GetProcAddress(PLib, 'FreeString');
  121. if (P2 <> nil) and (P3 <> nil) and (P4 <> nil) and (P5 <> nil) and (P6 <> nil) and (P7 <> nil) then
  122. begin
  123. FLastError := TLastError(P2);
  124. FToTextFile := TToTextFile(P3);
  125. FToString := TToString(P4);
  126. FHttpToTextFile := THttpToTextFile(P5);
  127. FHttpToString := THttpToString(P6);
  128. FFreeString := TFreeString(P7);
  129. Result := True;
  130. end
  131. else begin
  132. Result := False;
  133. end;
  134. end;
  135. // DLL初始化和设置软件授权
  136. procedure LoadAndAuth();
  137. var
  138. P1, P2: Pointer;
  139. Load: TLoad;
  140. Auth: TAuth;
  141. TempDir: RawByteString;
  142. Corp, LicTest: RawByteString;
  143. begin
  144. P1 := GetProcAddress(PLib, 'Load');
  145. P2 := GetProcAddress(PLib, 'Auth');
  146. if (P1 <> nil) and (P2 <> nil) then begin
  147. Load := TLoad(P1);
  148. // DLL工作的临时文件夹 ,需要程序对此文件夹有读写文件夹权限
  149. TempDir := Utf8Encode('tmp\');
  150. Load(PRawByteString(TempDir));
  151. //
  152. Auth := TAuth(P2);
  153. // 调用软件注册,免费版也需要调用此函数(传空值即可)
  154. Corp := Utf8Encode('Beij Gaya');
  155. LicTest := Utf8Encode('');
  156. Auth(PRawByteString(Corp), PRawByteString(LicTest));
  157. end;
  158. end;
  159. procedure Unload();
  160. var
  161. P: Pointer;
  162. Unload: TUnload;
  163. begin
  164. P := GetProcAddress(PLib, 'Unload');
  165. if (P <> nil) then begin
  166. Unload := TUnload(P);
  167. Unload();
  168. end;
  169. end;
  170. // 根据错误类型返回错误信息
  171. function ErrText(Code: Cardinal): string;
  172. begin
  173. case Code of
  174. TFE_OK:
  175. Result := 'ok';
  176. TFE_UNKNOW:
  177. Result := '未知错误';
  178. TFE_FILE_NOTEXIST:
  179. Result := '提取源文件不存在';
  180. TFE_SAVE_ERROR:
  181. Result := '保存目标文件失败';
  182. TFE_OUTSIZE:
  183. Result := '提取的源文件超出设置的大小范围';
  184. TFE_UNSUPPORTED:
  185. Result := '不支持的提取文件格式';
  186. TFE_ERROR_INTERFACE:
  187. Result := '得到接口失败';
  188. TFE_HTTP_ERR:
  189. Result := 'HTTP下载文件失败';
  190. TFE_HTTP_FILE_NULL:
  191. Result := 'HTTP文件为空';
  192. TFE_LICENCE_ERR:
  193. Result := '软件许可错误';
  194. end;
  195. end;
  196. // 调用DLL函数
  197. function FileToText(SourceFile, OutTextFile: string): Cardinal;
  198. var
  199. InUtf8Name, OutUtf8Name: RawByteString;
  200. begin
  201. // 文件名称要UTF-8编码
  202. InUtf8Name := Utf8Encode(SourceFile);
  203. OutUtf8Name := Utf8Encode(OutTextFile);
  204. Result := FToTextFile(PRawByteString(InUtf8Name), PRawByteString(OutUtf8Name));
  205. end;
  206. // 测试提取正文,保存到目标文本文件
  207. procedure FileToTextTest();
  208. var
  209. R: Cardinal;
  210. P: Pointer;
  211. S: RawByteString;
  212. Err, Err2: string;
  213. begin
  214. Writeln(TimeToStr(Now) + ' -- start');
  215. R := FileToText('D:\graccvs\files\简可信模板OCR识别工具帮助.docx', 'D:\graccvs\files\grcv001.txt');
  216. if R = 0 then
  217. Writeln(TimeToStr(Now) + ' -- end')
  218. else begin
  219. // 得到错误方式1: 根据R值调用函数ErrText得到具体错误信息, 此方式速度快
  220. Err := ErrText(R);
  221. Writeln(Err);
  222. // 方式2:调用DLL函数,得到具体错误信息, 此方式错误信息更加准确
  223. P := FLastError();
  224. S := RawByteString(PAnsiChar(P));
  225. Err2 := Utf8ToWideString(S);
  226. //
  227. Writeln(Err2)
  228. end;
  229. end;
  230. // 调用DLL函数
  231. function FileToString(SourceFile: string; var Err: string): string;
  232. var
  233. InUtf8Name, S: RawByteString;
  234. P: Pointer;
  235. Index, R: Integer;
  236. begin
  237. // 文件名称要UTF-8编码
  238. InUtf8Name := Utf8Encode(SourceFile);
  239. P := FToString(PRawByteString(InUtf8Name));
  240. if P <> nil then begin
  241. try
  242. S := PAnsiChar(P);
  243. Result := Utf8ToWideString(S);
  244. finally
  245. FFreeString(P);
  246. end;
  247. // 如果得到正文失败, 返回格式为 @ErrCode:x
  248. // x为整数,可以根据x值调用函数ErrText得到具体错误信息
  249. Index := Pos('@ErrCode:', Result);
  250. if Index > 0 then begin
  251. R := StrToIntDef(Copy(Result, Index + 9, 1), 0);
  252. Err := ErrText(R);
  253. end
  254. else
  255. Err := '';
  256. end
  257. else begin
  258. Result := '';
  259. Err := '得到文本失败';
  260. end;
  261. end;
  262. // 测试提取正文
  263. procedure ToStringText();
  264. var
  265. S, Err: string;
  266. begin
  267. Writeln(TimeToStr(Now) + ' -- start');
  268. //
  269. S := FileToString('D:\graccvs\files\Adobe Intro.ofd', Err);
  270. if Err <> '' then
  271. Writeln(Err)
  272. else
  273. Writeln(S);
  274. end;
  275. // Http文件提取文件正文
  276. function HttpToString(Url, FileExt: string; Timeout: Integer; var Err: string): string;
  277. var
  278. InUtf8Url, FileExtUtf8, S: RawByteString;
  279. P: Pointer;
  280. Index, R: Integer;
  281. begin
  282. // 文件名称要UTF-8编码
  283. InUtf8Url := Utf8Encode(Url);
  284. // 要提取的文件类型,比如 .docx, .ppt, .pdf, .html 等文件后缀
  285. FileExtUtf8 := Utf8Encode(FileExt);
  286. // 调用DLL中HttpToString函数, Timeout为超时设

    原文:https://blog.csdn.net/kkyy2021/article/details/122647260

联系站长

QQ:769220720

Copyright © SibooSoft All right reserved 津ICP备19011444号